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
008 * Development and Distribution License("CDDL") (collectively, the
009 * "License"). You may not use this file except in compliance with the
010 * License. You can obtain a copy of the License at
011 * http://www.netbeans.org/cddl-gplv2.html
012 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013 * specific language governing permissions and limitations under the
014 * License. When distributing the software, include this License Header
015 * Notice in each file and include the License file at
016 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017 * particular file as subject to the "Classpath" exception as provided
018 * by Sun in the GPL Version 2 section of the License file that
019 * accompanied this code. If applicable, add the following below the
020 * License Header, with the fields enclosed by brackets [] replaced by
021 * your own identifying information:
022 * "Portions Copyrighted [year] [name of copyright owner]"
023 *
024 * If you wish your version of this file to be governed by only the CDDL
025 * or only the GPL Version 2, indicate your decision by adding
026 * "[Contributor] elects to include this software in this distribution
027 * under the [CDDL or GPL Version 2] license." If you do not indicate a
028 * single choice of license, a recipient has the option to distribute
029 * your version of this file under either the CDDL, the GPL Version 2 or
030 * to extend the choice of license to its licensees as provided above.
031 * However, if you add GPL Version 2 code and therefore, elected the GPL
032 * Version 2 license, then the option applies only if the new code is
033 * made subject to such option by the copyright holder.
034 *
035 * Contributor(s):
036 *
037 * Portions Copyrighted 2008 Sun Microsystems, Inc.
038 */
039
040 package org.netbeans.modules.j2ee.deployment.impl;
041
042 import java.util.ArrayList;
043 import java.util.Collection;
044 import java.util.Collections;
045 import java.util.List;
046 import java.util.Set;
047 import javax.enterprise.deploy.spi.DeploymentManager;
048 import javax.enterprise.deploy.spi.Target;
049 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
050 import javax.enterprise.deploy.spi.factories.DeploymentFactory;
051 import javax.swing.event.ChangeEvent;
052 import javax.swing.event.ChangeListener;
053 import org.openide.util.Exceptions;
054 import org.openide.util.Lookup;
055 import org.openide.util.LookupEvent;
056 import org.openide.util.LookupListener;
057 import org.openide.util.lookup.Lookups;
058
059 /**
060 *
061 * @author Petr Hejl
062 */
063 public class ServerInstanceLookup extends Lookup {
064
065 private final ServerInstance instance;
066
067 private final DeploymentFactory factory;
068
069 private final Target target;
070
071 public ServerInstanceLookup(ServerInstance instance,
072 DeploymentFactory factory, Target target) {
073
074 assert instance != null;
075 assert factory != null;
076
077 this .instance = instance;
078 this .factory = factory;
079 this .target = target;
080 }
081
082 @Override
083 public <T> T lookup(Class<T> clazz) {
084 if (DeploymentFactory.class.isAssignableFrom(clazz)) {
085 return clazz.cast(factory);
086 } else if (DeploymentManager.class.isAssignableFrom(clazz)) {
087 if (instance.isConnected()) {
088 return clazz.cast(instance.getDeploymentManager());
089 }
090 try {
091 return clazz.cast(instance
092 .getDisconnectedDeploymentManager());
093 } catch (DeploymentManagerCreationException dmce) {
094 Exceptions.printStackTrace(dmce);
095 }
096 } else if (target != null
097 && Target.class.isAssignableFrom(clazz)) {
098 return clazz.cast(target);
099 }
100 return null;
101 }
102
103 @Override
104 public <T> Lookup.Result<T> lookup(Lookup.Template<T> template) {
105 if (DeploymentFactory.class
106 .isAssignableFrom(template.getType())) {
107 return new SimpleResult(factory);
108 } else if (DeploymentManager.class.isAssignableFrom(template
109 .getType())) {
110 return (Lookup.Result<T>) new DeploymentManagerResult(
111 instance);
112 } else if (Target.class.isAssignableFrom(template.getType())) {
113 return new SimpleResult(target);
114 }
115
116 return new EmptyResult();
117 }
118
119 private static final class EmptyResult<T> extends Lookup.Result<T> {
120
121 @Override
122 public void addLookupListener(LookupListener l) {
123 }
124
125 @Override
126 public void removeLookupListener(LookupListener l) {
127 }
128
129 @Override
130 public Collection<? extends T> allInstances() {
131 return Collections.emptyList();
132 }
133 }
134
135 private abstract static class AbstractResult<T> extends
136 Lookup.Result<T> {
137
138 public AbstractResult() {
139 super ();
140 }
141
142 public abstract T getInstance();
143
144 @Override
145 public synchronized Collection<? extends T> allInstances() {
146 T instance = getInstance();
147 if (instance == null) {
148 return Collections.emptyList();
149 }
150 return Collections.singletonList(instance);
151 }
152
153 @Override
154 public synchronized Set<Class<? extends T>> allClasses() {
155 T instance = getInstance();
156 if (instance == null) {
157 return Collections.emptySet();
158 }
159 Class<T> clazz = (Class<T>) instance.getClass();
160 return Collections.<Class<? extends T>> singleton(clazz);
161 }
162
163 @Override
164 public synchronized Collection<? extends Item<T>> allItems() {
165 T instance = getInstance();
166 if (instance == null) {
167 return Collections.emptyList();
168 }
169 return Collections.singletonList(Lookups.lookupItem(
170 instance, null));
171 }
172 }
173
174 private static class SimpleResult<T> extends AbstractResult<T> {
175
176 private final T instance;
177
178 public SimpleResult(T instance) {
179 this .instance = instance;
180 }
181
182 @Override
183 public void addLookupListener(LookupListener l) {
184 }
185
186 @Override
187 public void removeLookupListener(LookupListener l) {
188 }
189
190 @Override
191 public T getInstance() {
192 return instance;
193 }
194 }
195
196 private static class DeploymentManagerResult extends
197 AbstractResult<DeploymentManager> implements ChangeListener {
198
199 private final ServerInstance instance;
200
201 private final List<LookupListener> listeners = new ArrayList<LookupListener>();
202
203 public DeploymentManagerResult(ServerInstance instance) {
204 this .instance = instance;
205 }
206
207 @Override
208 public void addLookupListener(LookupListener l) {
209 if (l == null) {
210 return;
211 }
212 synchronized (listeners) {
213 if (listeners.isEmpty()) {
214 instance.addManagerChangeListener(this );
215 }
216 listeners.add(l);
217 }
218 }
219
220 @Override
221 public void removeLookupListener(LookupListener l) {
222 if (l == null) {
223 return;
224 }
225 synchronized (listeners) {
226 listeners.remove(l);
227 if (listeners.isEmpty()) {
228 instance.removeManagerChangeListener(this );
229 }
230 }
231 }
232
233 // this can fire fake events in rare cases
234 public void stateChanged(ChangeEvent e) {
235 List<LookupListener> toFire = new ArrayList<LookupListener>();
236 synchronized (listeners) {
237 toFire.addAll(listeners);
238 }
239
240 for (LookupListener listener : toFire) {
241 listener.resultChanged(new LookupEvent(this ));
242 }
243
244 }
245
246 @Override
247 public DeploymentManager getInstance() {
248 if (instance.isConnected()) {
249 return instance.getDeploymentManager();
250 }
251 try {
252 return instance.getDisconnectedDeploymentManager();
253 } catch (DeploymentManagerCreationException ex) {
254 Exceptions.printStackTrace(ex);
255 }
256 return null;
257 }
258 }
259 }
|