001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.api.server;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.xml.ws.api.message.Message;
041: import com.sun.xml.ws.api.message.Packet;
042: import com.sun.xml.ws.resources.ServerMessages;
043: import com.sun.xml.ws.resources.WsservletMessages;
044: import com.sun.xml.ws.server.ServerRtException;
045: import com.sun.xml.ws.server.SingletonResolver;
046:
047: import javax.xml.ws.Provider;
048: import javax.xml.ws.WebServiceContext;
049: import javax.xml.ws.WebServiceException;
050: import java.lang.annotation.Annotation;
051: import java.lang.reflect.InvocationTargetException;
052: import java.lang.reflect.Method;
053: import java.util.logging.Level;
054: import java.util.logging.Logger;
055:
056: /**
057: * Determines the instance that serves
058: * the given request packet.
059: *
060: * <p>
061: * The JAX-WS spec always use a singleton instance
062: * to serve all the requests, but this hook provides
063: * a convenient way to route messages to a proper receiver.
064: *
065: * <p>
066: * Externally, an instance of {@link InstanceResolver} is
067: * associated with {@link WSEndpoint}.
068: *
069: * <h2>Possible Uses</h2>
070: * <p>
071: * One can use WS-Addressing message properties to
072: * decide which instance to deliver a message. This
073: * would be an important building block for a stateful
074: * web services.
075: *
076: * <p>
077: * One can associate an instance of a service
078: * with a specific WS-RM session.
079: *
080: * @author Kohsuke Kawaguchi
081: */
082: public abstract class InstanceResolver<T> {
083: /**
084: * Decides which instance of 'T' serves the given request message.
085: *
086: * <p>
087: * This method is called concurrently by multiple threads.
088: * It is also on a criticail path that affects the performance.
089: * A good implementation should try to avoid any synchronization,
090: * and should minimize the amount of work as much as possible.
091: *
092: * @param request
093: * Always non-null. Represents the request message to be served.
094: * The caller may not consume the {@link Message}.
095: */
096: public abstract @NotNull
097: T resolve(@NotNull
098: Packet request);
099:
100: /**
101: * Called by the default {@link Invoker} after the method call is done.
102: * This gives {@link InstanceResolver} a chance to do clean up.
103: *
104: * <p>
105: * Alternatively, one could override {@link #createInvoker()} to
106: * create a custom invoker to do this in more flexible way.
107: *
108: * <p>
109: * The default implementation is a no-op.
110: *
111: * @param request
112: * The same request packet given to {@link #resolve(Packet)} method.
113: * @param servant
114: * The object returned from the {@link #resolve(Packet)} method.
115: * @since 2.1.2
116: */
117: public void postInvoke(@NotNull
118: Packet request, @NotNull
119: T servant) {
120: }
121:
122: /**
123: * Called by {@link WSEndpoint} when it's set up.
124: *
125: * <p>
126: * This is an opportunity for {@link InstanceResolver}
127: * to do a endpoint-specific initialization process.
128: *
129: * @param wsc
130: * The {@link WebServiceContext} instance to be injected
131: * to the user instances (assuming {@link InstanceResolver}
132: */
133: public void start(@NotNull
134: WSWebServiceContext wsc, @NotNull
135: WSEndpoint endpoint) {
136: // backward compatibility
137: start(wsc);
138: }
139:
140: /**
141: * @deprecated
142: * Use {@link #start(WSWebServiceContext,WSEndpoint)}.
143: */
144: public void start(@NotNull
145: WebServiceContext wsc) {
146: }
147:
148: /**
149: * Called by {@link WSEndpoint}
150: * when {@link WSEndpoint#dispose()} is called.
151: *
152: * This allows {@link InstanceResolver} to do final clean up.
153: *
154: * <p>
155: * This method is guaranteed to be only called once by {@link WSEndpoint}.
156: */
157: public void dispose() {
158: }
159:
160: /**
161: * Creates a {@link InstanceResolver} implementation that always
162: * returns the specified singleton instance.
163: */
164: public static <T> InstanceResolver<T> createSingleton(T singleton) {
165: assert singleton != null;
166: InstanceResolver ir = createFromInstanceResolverAnnotation(singleton
167: .getClass());
168: if (ir == null)
169: ir = new SingletonResolver<T>(singleton);
170: return ir;
171: }
172:
173: /**
174: * @deprecated
175: * This is added here because a Glassfish integration happened
176: * with this signature. Please do not use this. Will be removed
177: * after the next GF integration.
178: */
179: public static <T> InstanceResolver<T> createDefault(@NotNull
180: Class<T> clazz, boolean bool) {
181: return createDefault(clazz);
182: }
183:
184: /**
185: * Creates a default {@link InstanceResolver} that serves the given class.
186: */
187: public static <T> InstanceResolver<T> createDefault(@NotNull
188: Class<T> clazz) {
189: InstanceResolver<T> ir = createFromInstanceResolverAnnotation(clazz);
190: if (ir == null)
191: ir = new SingletonResolver<T>(createNewInstance(clazz));
192: return ir;
193: }
194:
195: /**
196: * Checks for {@link InstanceResolverAnnotation} and creates an instance resolver from it if any.
197: * Otherwise null.
198: */
199: private static <T> InstanceResolver<T> createFromInstanceResolverAnnotation(
200: @NotNull
201: Class<T> clazz) {
202: for (Annotation a : clazz.getAnnotations()) {
203: InstanceResolverAnnotation ira = a.annotationType()
204: .getAnnotation(InstanceResolverAnnotation.class);
205: if (ira == null)
206: continue;
207: Class<? extends InstanceResolver> ir = ira.value();
208: try {
209: return ir.getConstructor(Class.class)
210: .newInstance(clazz);
211: } catch (InstantiationException e) {
212: throw new WebServiceException(ServerMessages
213: .FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(ir
214: .getName(), a.annotationType(), clazz
215: .getName()));
216: } catch (IllegalAccessException e) {
217: throw new WebServiceException(ServerMessages
218: .FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(ir
219: .getName(), a.annotationType(), clazz
220: .getName()));
221: } catch (InvocationTargetException e) {
222: throw new WebServiceException(ServerMessages
223: .FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(ir
224: .getName(), a.annotationType(), clazz
225: .getName()));
226: } catch (NoSuchMethodException e) {
227: throw new WebServiceException(ServerMessages
228: .FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(ir
229: .getName(), a.annotationType(), clazz
230: .getName()));
231: }
232: }
233:
234: return null;
235: }
236:
237: protected static <T> T createNewInstance(Class<T> cl) {
238: try {
239: return cl.newInstance();
240: } catch (InstantiationException e) {
241: logger.log(Level.SEVERE, e.getMessage(), e);
242: throw new ServerRtException(WsservletMessages
243: .ERROR_IMPLEMENTOR_FACTORY_NEW_INSTANCE_FAILED(cl));
244: } catch (IllegalAccessException e) {
245: logger.log(Level.SEVERE, e.getMessage(), e);
246: throw new ServerRtException(WsservletMessages
247: .ERROR_IMPLEMENTOR_FACTORY_NEW_INSTANCE_FAILED(cl));
248: }
249: }
250:
251: /**
252: * Wraps this {@link InstanceResolver} into an {@link Invoker}.
253: */
254: public @NotNull
255: Invoker createInvoker() {
256: return new Invoker() {
257: @Override
258: public void start(@NotNull
259: WSWebServiceContext wsc, @NotNull
260: WSEndpoint endpoint) {
261: InstanceResolver.this .start(wsc, endpoint);
262: }
263:
264: @Override
265: public void dispose() {
266: InstanceResolver.this .dispose();
267: }
268:
269: @Override
270: public Object invoke(Packet p, Method m, Object... args)
271: throws InvocationTargetException,
272: IllegalAccessException {
273: T t = resolve(p);
274: try {
275: return m.invoke(t, args);
276: } finally {
277: postInvoke(p, t);
278: }
279: }
280:
281: @Override
282: public <U> U invokeProvider(@NotNull
283: Packet p, U arg) {
284: T t = resolve(p);
285: try {
286: return ((Provider<U>) t).invoke(arg);
287: } finally {
288: postInvoke(p, t);
289: }
290: }
291:
292: public String toString() {
293: return "Default Invoker over "
294: + InstanceResolver.this .toString();
295: }
296: };
297: }
298:
299: private static final Logger logger = Logger
300: .getLogger(com.sun.xml.ws.util.Constants.LoggingDomain
301: + ".server");
302: }
|