KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > ServiceType


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide;
21
22 import java.beans.Introspector JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import org.openide.util.Enumerations;
35 import org.openide.util.Exceptions;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.Lookup;
38
39 /** This class represents an abstract subclass for services
40 * (compilation, execution, debugging, etc.) that can be registered in
41 * the system.
42 *
43 * @author Jaroslav Tulach
44 * @deprecated The prefered way to register and lookup services
45  * is now {@link Lookup} as described in <a HREF="util/doc-files/api.html#lookup">
46  * services registration and lookup</a> page.
47 */

48 @Deprecated JavaDoc
49 public abstract class ServiceType extends Object JavaDoc implements Serializable JavaDoc, HelpCtx.Provider {
50     /** generated Serialized Version UID */
51     private static final long serialVersionUID = -7573598174423654252L;
52
53     /** Name of property for the name of the service type. */
54     public static final String JavaDoc PROP_NAME = "name"; // NOI18N
55
private static final Logger JavaDoc err = Logger.getLogger("org.openide.ServiceType"); // NOI18N
56

57     /** name of the service type */
58     private String JavaDoc name;
59
60     /** listeners support */
61     private transient PropertyChangeSupport JavaDoc supp;
62
63     /** Default human-presentable name of the service type.
64     * In the default implementation, taken from the bean descriptor.
65     * @return initial value of the human-presentable name
66     * @see java.beans.FeatureDescriptor#getDisplayName
67     */

68     protected String JavaDoc displayName() {
69         try {
70             return Introspector.getBeanInfo(getClass()).getBeanDescriptor().getDisplayName();
71         } catch (Exception JavaDoc e) {
72             // Catching IntrospectionException, but also maybe NullPointerException...?
73
Logger.getLogger(ServiceType.class.getName()).log(Level.WARNING, null, e);
74
75             return getClass().getName();
76         }
77     }
78
79     /** Method that creates a cloned instance of this object. Subclasses
80      * are encouraged to implement the {@link Cloneable}
81      * interface, in such case the <code>clone</code> method is called as a result
82      * of calling this method. If the subclass does not implement
83      * <code>Cloneable</code>, it is serialized and deserialized,
84      * thus new instance created.
85      *
86      * @return new instance
87      * @exception IllegalStateException if something goes wrong, but should not happen
88      * @deprecated Service instance files should instead be copied in order to clone them.
89      */

90     @Deprecated JavaDoc
91     public final ServiceType createClone() {
92         Exception JavaDoc anEx;
93
94         if (this instanceof Cloneable JavaDoc) {
95             try {
96                 return (ServiceType) clone();
97             } catch (CloneNotSupportedException JavaDoc ex) {
98                 anEx = ex;
99             }
100         } else {
101             try {
102                 org.openide.util.io.NbMarshalledObject m = new org.openide.util.io.NbMarshalledObject(this);
103
104                 return (ServiceType) m.get();
105             } catch (IOException JavaDoc ex) {
106                 anEx = ex;
107             } catch (ClassNotFoundException JavaDoc ex) {
108                 anEx = ex;
109             }
110         }
111
112         // the code can get here only if an exception occured
113
// moreover it should never happen that this code is executed
114
IllegalStateException JavaDoc ex = new IllegalStateException JavaDoc();
115
116         ex.initCause(anEx);
117         Exceptions.attachLocalizedMessage(ex, "Cannot createClone for " + this); // NOI18N
118

119         throw ex;
120     }
121
122     /** Correctly implements the clone operation on this object. In
123      * order to work really correctly, the subclass has to implement the
124      * Cloneable interface.
125      *
126      * @return a new cloned instance that does not have any listeners
127      * @deprecated Service instance files should instead be copied in order to clone them.
128      */

129     @Deprecated JavaDoc
130     protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
131         ServiceType t = (ServiceType) super.clone();
132
133         // clear listeners
134
t.supp = null;
135
136         // clear name
137
t.name = null;
138
139         return t;
140     }
141
142     /** Set the name of the service type.
143     * Usually it suffices to override {@link #displayName},
144     * or just to provide a {@link java.beans.BeanDescriptor} for the class.
145     * @param name the new human-presentable name
146     */

147     public void setName(String JavaDoc name) {
148         String JavaDoc old = this.name;
149         this.name = name;
150
151         if (supp != null) {
152             supp.firePropertyChange(PROP_NAME, old, name);
153         }
154     }
155
156     /** Get the name of the service type.
157     * The default value is given by {@link #displayName}.
158     * @return a human-presentable name for the service type
159     */

160     public String JavaDoc getName() {
161         return (name == null) ? displayName() : name;
162     }
163
164     /** Get context help for this service type.
165     * @return context help
166     */

167     public abstract HelpCtx getHelpCtx();
168
169     /** Add a property change listener.
170     * @param l the listener to add
171     */

172     public final synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
173         if (supp == null) {
174             supp = new PropertyChangeSupport JavaDoc(this);
175         }
176
177         supp.addPropertyChangeListener(l);
178     }
179
180     /** Remove a property change listener.
181     * @param l the listener to remove
182     */

183     public final void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
184         if (supp != null) {
185             supp.removePropertyChangeListener(l);
186         }
187     }
188
189     /** Fire information about change of a property in the service type.
190     * @param name name of the property
191     * @param o old value
192     * @param n new value
193     */

194     protected final void firePropertyChange(String JavaDoc name, Object JavaDoc o, Object JavaDoc n) {
195         if (supp != null) {
196             supp.firePropertyChange(name, o, n);
197         }
198     }
199
200     /**
201      * The registry of all services. This class is provided by the NetBeans core
202     * and should hold all of the services registered to the system.
203     * <P>
204     * This class can be serialized to securely save settings of all
205     * services in the system.
206     * @deprecated Use lookup instead.
207     */

208     @Deprecated JavaDoc
209     public static abstract class Registry implements Serializable JavaDoc {
210         /** suid */
211         final static long serialVersionUID = 8721000770371416481L;
212
213         /** Get all available services managed by the engine.
214         * @return an enumeration of {@link ServiceType}s
215         */

216         public abstract Enumeration JavaDoc<ServiceType> services();
217
218         /** Get all available services that are assignable to the given superclass.
219         * @param clazz the class that all services should be subclass of
220         * @return an enumeration of all matching {@link ServiceType}s
221         */

222         public <T extends ServiceType> Enumeration JavaDoc<T> services(final Class JavaDoc<T> clazz) {
223             class IsInstance implements Enumerations.Processor<ServiceType,T> {
224                 public T process(ServiceType obj, Collection JavaDoc ignore) {
225                     return clazz.isInstance(obj) ? clazz.cast(obj) : null;
226                 }
227             }
228
229             return Enumerations.filter(services(), new IsInstance());
230         }
231
232         /** Getter for list of all service types.
233         * @return a list of {@link ServiceType}s
234         */

235         public abstract List JavaDoc getServiceTypes();
236
237         /** Setter for list of service types. This permits changing
238         * instances of the objects but only within the types that are already registered
239         * in the system by manifest sections. If an instance of any other type
240         * is in the list it is ignored.
241         *
242         * @param arr a list of {@link ServiceType}s
243         * @deprecated Better to change service instance files instead.
244         */

245         @Deprecated JavaDoc
246         public abstract void setServiceTypes(List JavaDoc arr);
247
248         /** Find the service type implemented as a given class.
249          * The whole registry is searched for a service type of that exact class (subclasses do not count).
250         * <P>
251         * This could be used during (de-)serialization
252         * of a service type: only store its class name
253         * and then try to find the type implemented by that class later.
254         *
255         * @param clazz the class of the service type looked for
256         * @return the desired type or <code>null</code> if it does not exist
257          * @deprecated Just use lookup.
258         */

259         @Deprecated JavaDoc
260         public ServiceType find(Class JavaDoc clazz) {
261             Enumeration JavaDoc en = services();
262
263             while (en.hasMoreElements()) {
264                 Object JavaDoc o = en.nextElement();
265
266                 if (o.getClass() == clazz) {
267                     return (ServiceType) o;
268                 }
269             }
270
271             return null;
272         }
273
274         /** Find a service type of a supplied name in the registry.
275         * <P>
276         * This could be used during (de-)serialization
277         * of a service type: only store its name
278         * and then try to find the type later.
279         *
280         * @param name (display) name of service type to find
281         * @return the desired type or <code>null</code> if it does not exist
282         */

283         public ServiceType find(String JavaDoc name) {
284             Enumeration JavaDoc en = services();
285
286             while (en.hasMoreElements()) {
287                 ServiceType o = (ServiceType) en.nextElement();
288
289                 if (name.equals(o.getName())) {
290                     return o;
291                 }
292             }
293
294             return null;
295         }
296     }
297
298     /** Handle for a service type. This is a serializable class that should be used
299     * to store types and to recreate them after deserialization.
300     * @deprecated The prefered way to register and lookup services
301     * is now {@link Lookup} as described in <a HREF="util/doc-files/api.html#lookup">
302     * services registration and lookup</a> page.
303     */

304     @Deprecated JavaDoc
305     public static final class Handle extends Object JavaDoc implements java.io.Serializable JavaDoc {
306         /** generated Serialized Version UID */
307         static final long serialVersionUID = 7233109534462148872L;
308
309         /** name executor */
310         private String JavaDoc name;
311
312         /** name of class of the executor */
313         private String JavaDoc className;
314
315         /** kept ServiceType may be <tt>null</tt> after deserialization */
316         private transient ServiceType serviceType;
317
318         /** Create a new handle for an service.
319         * @param ex the service to store a handle for
320         */

321         public Handle(ServiceType ex) {
322             name = ex.getName();
323             className = ex.getClass().getName();
324             serviceType = ex;
325         }
326
327         /** Find the service for this handle.
328         * @return the reconstituted service type, or <code>null</code> in case of problems
329         */

330         public ServiceType getServiceType() {
331             if (serviceType == null) {
332                 // the class to search for
333
Class JavaDoc<? extends ServiceType> clazz;
334
335                 // the first subclass of ServiceType to search for
336
Class JavaDoc<?> serviceTypeClass;
337
338                 // try to find it by class
339
try {
340                     serviceTypeClass = Class.forName(className, true, Lookup.getDefault().lookup(ClassLoader JavaDoc.class));
341                     clazz = serviceTypeClass.asSubclass(ServiceType.class);
342
343                     while (serviceTypeClass.getSuperclass() != ServiceType.class) {
344                         serviceTypeClass = serviceTypeClass.getSuperclass();
345                     }
346                 } catch (ClassNotFoundException JavaDoc ex) {
347                     // #32140 - do not notify user about this exception. This exception
348
// should be only thrown when module providing the service
349
// was uninstalled and in that case the exception must be ignored.
350
err.log(Level.FINE, "Service not found", ex); //NOI18N
351

352                     // nothing better to use
353
clazz = ServiceType.class;
354                     serviceTypeClass = ServiceType.class;
355                 }
356
357                 // try to find the executor by name
358
ServiceType.Registry r = Lookup.getDefault().lookup(ServiceType.Registry.class);
359                 Enumeration JavaDoc en = r.services(clazz);
360                 ServiceType some = r.find(clazz);
361
362                 while (en.hasMoreElements()) {
363                     ServiceType t = (ServiceType) en.nextElement();
364
365                     if (!serviceTypeClass.isInstance(t)) {
366                         // ignore non instances
367
continue;
368                     }
369
370                     String JavaDoc n = t.getName();
371
372                     if ((n != null) && n.equals(name)) {
373                         return t;
374                     }
375
376                     // remember it for later use
377
if ((some == null) || ((some.getClass() != clazz) && (t.getClass() == clazz))) {
378                         // remember the best match
379
some = t;
380                     }
381                 }
382
383                 // if clazz does not exist and there is no service with same name -> return null
384
if (serviceTypeClass == ServiceType.class) {
385                     return null;
386                 }
387
388                 return some;
389             }
390
391             return serviceType;
392         }
393
394         /** Old compatibility version.
395         */

396         private void readObject(ObjectInputStream JavaDoc ois)
397         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
398             name = (String JavaDoc) ois.readObject();
399
400             String JavaDoc clazz = (String JavaDoc) ois.readObject();
401             className = (clazz == null) ? null : org.openide.util.Utilities.translate(clazz);
402         }
403
404         /** Has also save the object.
405         */

406         private void writeObject(ObjectOutputStream JavaDoc oos)
407         throws IOException JavaDoc {
408             oos.writeObject(name);
409             oos.writeObject(className);
410         }
411
412         // for debugging purposes
413
public String JavaDoc toString() {
414             return "Handle[" + className + ":" + name + "]"; // NOI18N
415
}
416     }
417 }
418
Popular Tags