View Javadoc

1   /**
2    * Copyright (C) 2010 Daniel Manzke <daniel.manzke@googlemail.com>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.onami.autobind.integrations.metro;
17  
18  import javax.xml.ws.WebServiceContext;
19  
20  import com.google.inject.AbstractModule;
21  import com.google.inject.Guice;
22  import com.google.inject.Injector;
23  import com.google.inject.Module;
24  import com.sun.istack.NotNull;
25  import com.sun.xml.ws.api.message.Packet;
26  import com.sun.xml.ws.api.server.ResourceInjector;
27  import com.sun.xml.ws.api.server.WSEndpoint;
28  import com.sun.xml.ws.api.server.WSWebServiceContext;
29  import com.sun.xml.ws.server.AbstractMultiInstanceResolver;
30  
31  public class AutomaticGuiceManager<T> extends AbstractMultiInstanceResolver<T> {
32  	private static Module startup;
33  	protected static Injector injector;
34  	protected ResourceInjector resourceInjector;
35  	protected WSWebServiceContext webServiceContext;
36  
37  	public AutomaticGuiceManager(@NotNull final Class<T> clazz) throws IllegalAccessException,
38  			InstantiationException {
39  		super(clazz);
40  	}
41  
42  	@Override
43  	public T resolve(@NotNull final Packet packet) {
44  		final T instance = injector.getInstance(this.clazz);
45  		resourceInjector.inject(this.webServiceContext, instance);
46  		return instance;
47  	}
48  
49  	@SuppressWarnings("rawtypes")
50  	@Override
51  	public synchronized void start(WSWebServiceContext wsc, WSEndpoint endpoint) {
52  		resourceInjector = getResourceInjector(endpoint);
53  		webServiceContext = wsc;
54  		if(injector == null){
55  			injector = Guice.createInjector(startup, new AbstractModule() {
56  				@Override
57  				protected void configure() {
58  					bind(WebServiceContext.class).toInstance(webServiceContext);
59  				}
60  			});
61  		}
62  	}
63  	
64  	public static void inject(Module startup){
65  		AutomaticGuiceManager.startup = startup;
66  	}
67  }