KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > baseadaptor > AdaptorUtil


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.internal.baseadaptor;
13
14 import java.io.*;
15 import java.net.URL JavaDoc;
16 import java.util.Dictionary JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import org.eclipse.core.runtime.internal.adaptor.EclipseAdaptorMsg;
19 import org.eclipse.osgi.baseadaptor.BaseData;
20 import org.eclipse.osgi.framework.debug.Debug;
21 import org.eclipse.osgi.framework.internal.core.Constants;
22 import org.eclipse.osgi.framework.util.Headers;
23 import org.eclipse.osgi.util.NLS;
24 import org.osgi.framework.*;
25
26 /**
27  * A utility class with some generally useful static methods for adaptor hook implementations
28  */

29 public class AdaptorUtil {
30     /** The NULL tag used in bundle storage */
31     public static final byte NULL = 0;
32     /** The OBJECT tag used in bundle storage */
33     public static final byte OBJECT = 1;
34
35     /**
36      * Does a recursive copy of one directory to another.
37      * @param inDir input directory to copy.
38      * @param outDir output directory to copy to.
39      * @throws IOException if any error occurs during the copy.
40      */

41     public static void copyDir(File inDir, File outDir) throws IOException {
42         String JavaDoc[] files = inDir.list();
43         if (files != null && files.length > 0) {
44             outDir.mkdir();
45             for (int i = 0; i < files.length; i++) {
46                 File inFile = new File(inDir, files[i]);
47                 File outFile = new File(outDir, files[i]);
48                 if (inFile.isDirectory()) {
49                     copyDir(inFile, outFile);
50                 } else {
51                     InputStream in = new FileInputStream(inFile);
52                     readFile(in, outFile);
53                 }
54             }
55         }
56     }
57
58     /**
59      * Read a file from an InputStream and write it to the file system.
60      *
61      * @param in InputStream from which to read.
62      * @param file output file to create.
63      * @exception IOException
64      */

65     public static void readFile(InputStream in, File file) throws IOException {
66         FileOutputStream fos = null;
67         try {
68             fos = new FileOutputStream(file);
69
70             byte buffer[] = new byte[1024];
71             int count;
72             while ((count = in.read(buffer, 0, buffer.length)) > 0) {
73                 fos.write(buffer, 0, count);
74             }
75
76             fos.close();
77             fos = null;
78
79             in.close();
80             in = null;
81         } catch (IOException e) {
82             // close open streams
83
if (fos != null) {
84                 try {
85                     fos.close();
86                 } catch (IOException ee) {
87                     // nothing to do here
88
}
89             }
90
91             if (in != null) {
92                 try {
93                     in.close();
94                 } catch (IOException ee) {
95                     // nothing to do here
96
}
97             }
98
99             if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
100                 Debug.println("Unable to read file"); //$NON-NLS-1$
101
Debug.printStackTrace(e);
102             }
103
104             throw e;
105         }
106     }
107
108     /**
109      * This function performs the equivalent of "rm -r" on a file or directory.
110      *
111      * @param file file or directory to delete
112      * @return false is the specified files still exists, true otherwise.
113      */

114     public static boolean rm(File file) {
115         if (file.exists()) {
116             if (file.isDirectory()) {
117                 String JavaDoc list[] = file.list();
118                 if (list != null) {
119                     int len = list.length;
120                     for (int i = 0; i < len; i++) {
121                         // we are doing a lot of garbage collecting here
122
rm(new File(file, list[i]));
123                     }
124                 }
125             }
126             if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
127                 if (file.isDirectory()) {
128                     Debug.println("rmdir " + file.getPath()); //$NON-NLS-1$
129
} else {
130                     Debug.println("rm " + file.getPath()); //$NON-NLS-1$
131
}
132             }
133
134             boolean success = file.delete();
135
136             if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
137                 if (!success) {
138                     Debug.println(" rm failed!!"); //$NON-NLS-1$
139
}
140             }
141
142             return (success);
143         }
144         return (true);
145     }
146
147     public static String JavaDoc readString(DataInputStream in, boolean intern) throws IOException {
148         byte type = in.readByte();
149         if (type == NULL)
150             return null;
151         return intern ? in.readUTF().intern() : in.readUTF();
152     }
153
154     public static void writeStringOrNull(DataOutputStream out, String JavaDoc string) throws IOException {
155         if (string == null)
156             out.writeByte(NULL);
157         else {
158             out.writeByte(OBJECT);
159             out.writeUTF(string);
160         }
161     }
162
163     public static Version loadVersion(DataInputStream in) throws IOException {
164         String JavaDoc versionString = readString(in, false);
165         try {
166             return Version.parseVersion(versionString);
167         } catch (IllegalArgumentException JavaDoc e) {
168             return new InvalidVersion(versionString);
169         }
170     }
171
172     /**
173      * Register a service object.
174      * @param name the service class name
175      * @param service the service object
176      * @param context the registering bundle context
177      * @return the service registration object
178      */

179     public static ServiceRegistration register(String JavaDoc name, Object JavaDoc service, BundleContext context) {
180         Hashtable JavaDoc properties = new Hashtable JavaDoc(7);
181         Dictionary JavaDoc headers = context.getBundle().getHeaders();
182         properties.put(Constants.SERVICE_VENDOR, headers.get(Constants.BUNDLE_VENDOR));
183         properties.put(Constants.SERVICE_RANKING, new Integer JavaDoc(Integer.MAX_VALUE));
184         properties.put(Constants.SERVICE_PID, context.getBundle().getBundleId() + "." + service.getClass().getName()); //$NON-NLS-1$
185
return context.registerService(name, service, properties);
186     }
187
188     public static Dictionary JavaDoc loadManifestFrom(BaseData bundledata) throws BundleException {
189         URL JavaDoc url = bundledata.getEntry(Constants.OSGI_BUNDLE_MANIFEST);
190         if (url == null)
191             return null;
192         try {
193             return Headers.parseManifest(url.openStream());
194         } catch (IOException e) {
195             throw new BundleException(NLS.bind(EclipseAdaptorMsg.ECLIPSE_DATA_ERROR_READING_MANIFEST, bundledata.getLocation()), e);
196         }
197     }
198
199 }
200
Popular Tags