1 11 12 package org.eclipse.osgi.internal.baseadaptor; 13 14 import java.io.*; 15 import java.net.URL ; 16 import java.util.Dictionary ; 17 import java.util.Hashtable ; 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 29 public class AdaptorUtil { 30 31 public static final byte NULL = 0; 32 33 public static final byte OBJECT = 1; 34 35 41 public static void copyDir(File inDir, File outDir) throws IOException { 42 String [] 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 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 if (fos != null) { 84 try { 85 fos.close(); 86 } catch (IOException ee) { 87 } 89 } 90 91 if (in != null) { 92 try { 93 in.close(); 94 } catch (IOException ee) { 95 } 97 } 98 99 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 100 Debug.println("Unable to read file"); Debug.printStackTrace(e); 102 } 103 104 throw e; 105 } 106 } 107 108 114 public static boolean rm(File file) { 115 if (file.exists()) { 116 if (file.isDirectory()) { 117 String list[] = file.list(); 118 if (list != null) { 119 int len = list.length; 120 for (int i = 0; i < len; i++) { 121 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()); } else { 130 Debug.println("rm " + file.getPath()); } 132 } 133 134 boolean success = file.delete(); 135 136 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 137 if (!success) { 138 Debug.println(" rm failed!!"); } 140 } 141 142 return (success); 143 } 144 return (true); 145 } 146 147 public static String 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 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 versionString = readString(in, false); 165 try { 166 return Version.parseVersion(versionString); 167 } catch (IllegalArgumentException e) { 168 return new InvalidVersion(versionString); 169 } 170 } 171 172 179 public static ServiceRegistration register(String name, Object service, BundleContext context) { 180 Hashtable properties = new Hashtable (7); 181 Dictionary headers = context.getBundle().getHeaders(); 182 properties.put(Constants.SERVICE_VENDOR, headers.get(Constants.BUNDLE_VENDOR)); 183 properties.put(Constants.SERVICE_RANKING, new Integer (Integer.MAX_VALUE)); 184 properties.put(Constants.SERVICE_PID, context.getBundle().getBundleId() + "." + service.getClass().getName()); return context.registerService(name, service, properties); 186 } 187 188 public static Dictionary loadManifestFrom(BaseData bundledata) throws BundleException { 189 URL 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 |