KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com4j > COM4J


1 package com4j;
2
3 import java.io.File JavaDoc;
4 import java.lang.reflect.Proxy JavaDoc;
5 import java.net.URL JavaDoc;
6
7 /**
8  * The root of the COM4J library.
9  *
10  * <p>
11  * Provides various global services that don't fit into the rest of classes.
12  *
13  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
14  */

15 public abstract class COM4J {
16     private COM4J() {} // no instanciation allowed
17

18     /**
19      * Creates a new COM object of the given CLSID and returns
20      * it in a wrapped interface.
21      *
22      * @param primaryInterface
23      * The created COM object is returned as this interface.
24      * Must be non-null. Passing in {@link Com4jObject} allows
25      * the caller to create a new instance without knowing
26      * its primary interface.
27      * @param clsid
28      * The CLSID of the COM object to be created. Must be non-null.
29      *
30      * @return
31      * non-null valid object.
32      *
33      * @throws ComException
34      * if the instanciation fails.
35      */

36     public static<T extends Com4jObject>
37     T createInstance( Class JavaDoc<T> primaryInterface, GUID clsid ) throws ComException {
38         return createInstance(primaryInterface,clsid.toString());
39     }
40
41     /**
42      * Creates a new COM object of the given CLSID and returns
43      * it in a wrapped interface.
44      *
45      * @param primaryInterface
46      * The created COM object is returned as this interface.
47      * Must be non-null. Passing in {@link Com4jObject} allows
48      * the caller to create a new instance without knowing
49      * its primary interface.
50      * @param clsid
51      * The CLSID of the COM object in the
52      * "<tt>{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}</tt>" format,
53      * or the ProgID of the object (like "Microsoft.XMLParser.1.0")
54      *
55      * @return
56      * non-null valid object.
57      *
58      * @throws ComException
59      * if the instanciation fails.
60      */

61     public static<T extends Com4jObject>
62     T createInstance( Class JavaDoc<T> primaryInterface, String JavaDoc clsid ) throws ComException {
63
64         // create instance
65
return new CreateInstanceTask<T>(clsid,primaryInterface).execute();
66     }
67
68     private static class CreateInstanceTask<T extends Com4jObject> extends Task<T> {
69         private final String JavaDoc clsid;
70         private final Class JavaDoc<T> intf;
71
72         public CreateInstanceTask(String JavaDoc clsid, Class JavaDoc<T> intf) {
73             this.clsid = clsid;
74             this.intf = intf;
75         }
76
77         public T call() {
78             GUID iid = getIID(intf);
79             return Wrapper.create( intf, Native.createInstance(clsid,iid.v[0],iid.v[1]) );
80         }
81     }
82
83     /**
84      * Gets the interface GUID associated with the given interface.
85      *
86      * <p>
87      * This method retrieves the associated {@link IID} annotation from the
88      * interface and return it.
89      *
90      * @throws IllegalArgumentException
91      * if the interface doesn't have any {@link IID} annotation.
92      *
93      * @return
94      * always return no-null valid {@link GUID} object.
95      */

96     public static GUID getIID( Class JavaDoc<? extends Com4jObject> _interface ) {
97         IID iid = _interface.getAnnotation(IID.class);
98         if(iid==null)
99             throw new IllegalArgumentException JavaDoc(_interface.getName()+" doesn't have @IID annotation");
100         return new GUID(iid.value());
101     }
102
103     /**
104      * Loads a type library from a given file and returns its IUnknown.
105      *
106      * <p>
107      * Exposed for <tt>tlbimp</tt>.
108      */

109     public static Com4jObject loadTypeLibrary( final File JavaDoc typeLibraryFile ) {
110         return new Task<Com4jObject>() {
111             public Com4jObject call() {
112                 return Wrapper.create(
113                     Native.loadTypeLibrary(typeLibraryFile.getAbsolutePath()));
114             }
115         }.execute();
116     }
117
118     /**
119      * GUID of IUnknown.
120      */

121     public static final GUID IID_IUnknown = new GUID("{00000000-0000-0000-C000-000000000046}");
122
123     /**
124      * GUID of IDispatch.
125      */

126     public static final GUID IID_IDispatch = new GUID("{00020400-0000-0000-C000-000000000046}");
127
128
129
130     /**
131      * Registers a {@link ComObjectListener} to the current thread.
132      *
133      * <p>
134      * The registered listener will receive a notification each time
135      * a new proxy is created.
136      *
137      * @throws IllegalArgumentException
138      * If the listener is null or it is already registered.
139      *
140      * @see #removeListener(ComObjectListener)
141      */

142     public static void addListener( ComObjectListener listener ) {
143         ComThread.get().addListener(listener);
144     }
145
146     /**
147      * Removes a registered {@link ComObjectListener} from the current thread.
148      *
149      * @param listener
150      * this listner has to be registered via {@link #addListener(ComObjectListener)}.
151      *
152      * @throws IllegalArgumentException
153      * If the listener is not currently registered.
154      *
155      * @see #addListener(ComObjectListener)
156      */

157     public static void removeListener( ComObjectListener listener ) {
158         ComThread.get().removeListener(listener);
159     }
160
161     static int queryInterface( int ptr, GUID iid ) {
162         return Native.queryInterface(ptr,iid.v[0],iid.v[1]);
163     }
164
165     static Wrapper unwrap( Com4jObject obj ) {
166         if( obj instanceof Wrapper )
167             return (Wrapper)obj;
168         else
169             return (Wrapper)Proxy.getInvocationHandler(obj);
170     }
171
172     // called by the native side to get the raw pointer value of Com4jObject.
173
static int getPtr( Com4jObject obj ) {
174         if(obj==null) return 0;
175         return unwrap(obj).getPtr();
176     }
177
178     static {
179         loadNativeLibrary();
180         Native.init();
181     }
182
183     private static void loadNativeLibrary() {
184         try {
185             // load the native part of the code.
186
// first try java.library.path
187
System.loadLibrary("com4j");
188             return;
189         } catch( Throwable JavaDoc t ) {
190             ;
191         }
192
193         // try loading com4j.dll in the same directory as com4j.jar
194
URL JavaDoc res = COM4J.class.getClassLoader().getResource("com4j/COM4J.class");
195         String JavaDoc url = res.toExternalForm();
196         if(url.startsWith("jar://")) {
197             int idx = url.lastIndexOf('!');
198             String JavaDoc filePortion = url.substring(6,idx);
199             if(filePortion.startsWith("file://")) {
200                 File JavaDoc jarFile = new File JavaDoc(filePortion.substring(7));
201                 File JavaDoc dllFile = new File JavaDoc(jarFile.getParentFile(),"com4j.dll");
202                 System.load(dllFile.getPath());
203                 return;
204             }
205         }
206
207         throw new UnsatisfiedLinkError JavaDoc("Unable to load com4j.dll");
208     }
209 }
210
Popular Tags