|
1 /* |
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.server; |
|
27 |
|
28 import com.sun.xml.internal.ws.api.server.AbstractInstanceResolver; |
|
29 import com.sun.xml.internal.ws.api.server.InstanceResolver; |
|
30 import com.sun.xml.internal.ws.api.server.ResourceInjector; |
|
31 import com.sun.xml.internal.ws.api.server.WSEndpoint; |
|
32 import com.sun.xml.internal.ws.api.server.WSWebServiceContext; |
|
33 |
|
34 import javax.annotation.PostConstruct; |
|
35 import javax.annotation.PreDestroy; |
|
36 import java.lang.reflect.Method; |
|
37 |
|
38 /** |
|
39 * Partial implementation of {@link InstanceResolver} with code |
|
40 * to handle multiple instances per server. |
|
41 * |
|
42 * @author Kohsuke Kawaguchi |
|
43 */ |
|
44 public abstract class AbstractMultiInstanceResolver<T> extends AbstractInstanceResolver<T> { |
|
45 protected final Class<T> clazz; |
|
46 |
|
47 // fields for resource injection. |
|
48 private /*almost final*/ WSWebServiceContext webServiceContext; |
|
49 protected /*almost final*/ WSEndpoint owner; |
|
50 private final Method postConstructMethod; |
|
51 private final Method preDestroyMethod; |
|
52 private /*almost final*/ ResourceInjector resourceInjector; |
|
53 |
|
54 public AbstractMultiInstanceResolver(Class<T> clazz) { |
|
55 this.clazz = clazz; |
|
56 |
|
57 postConstructMethod = findAnnotatedMethod(clazz, PostConstruct.class); |
|
58 preDestroyMethod = findAnnotatedMethod(clazz, PreDestroy.class); |
|
59 } |
|
60 |
|
61 /** |
|
62 * Perform resource injection on the given instance. |
|
63 */ |
|
64 protected final void prepare(T t) { |
|
65 // we can only start creating new instances after the start method is invoked. |
|
66 assert webServiceContext!=null; |
|
67 |
|
68 resourceInjector.inject(webServiceContext,t); |
|
69 invokeMethod(postConstructMethod,t); |
|
70 } |
|
71 |
|
72 /** |
|
73 * Creates a new instance via the default constructor. |
|
74 */ |
|
75 protected final T create() { |
|
76 T t = createNewInstance(clazz); |
|
77 prepare(t); |
|
78 return t; |
|
79 } |
|
80 |
|
81 @Override |
|
82 public void start(WSWebServiceContext wsc, WSEndpoint endpoint) { |
|
83 resourceInjector = getResourceInjector(endpoint); |
|
84 this.webServiceContext = wsc; |
|
85 this.owner = endpoint; |
|
86 } |
|
87 |
|
88 protected final void dispose(T instance) { |
|
89 invokeMethod(preDestroyMethod,instance); |
|
90 } |
|
91 } |