1 11 package org.eclipse.core.internal.runtime; 12 13 import java.io.*; 14 import java.net.URL ; 15 import java.net.UnknownServiceException ; 16 import org.eclipse.core.internal.boot.PlatformURLConnection; 17 import org.eclipse.core.internal.boot.PlatformURLHandler; 18 import org.eclipse.osgi.service.datalocation.Location; 19 import org.eclipse.osgi.util.NLS; 20 21 public class PlatformURLConfigConnection extends PlatformURLConnection { 22 private static final String FILE_PROTOCOL = "file"; private static boolean isRegistered = false; 24 public static final String CONFIG = "config"; 26 private boolean parentConfiguration = false; 27 28 31 public PlatformURLConfigConnection(URL url) { 32 super(url); 33 } 34 35 38 protected URL resolve() throws IOException { 39 String spec = url.getFile().trim(); 40 if (spec.startsWith("/")) spec = spec.substring(1); 42 if (!spec.startsWith(CONFIG)) 43 throw new IOException(NLS.bind(CommonMessages.url_badVariant, url.toString())); 44 String path = spec.substring(CONFIG.length() + 1); 45 Activator activator = Activator.getDefault(); 47 if (activator == null) 48 throw new IOException(CommonMessages.activator_not_available); 49 Location localConfig = activator.getConfigurationLocation(); 50 Location parentConfig = localConfig.getParentLocation(); 51 URL localURL = new URL (localConfig.getURL(), path); 53 if (!FILE_PROTOCOL.equals(localURL.getProtocol()) || parentConfig == null) 54 return localURL; 56 File localFile = new File(localURL.getPath()); 57 if (localFile.exists()) 58 return localURL; 60 URL parentURL = new URL (parentConfig.getURL(), path); 62 if (FILE_PROTOCOL.equals(parentURL.getProtocol())) { 63 File parentFile = new File(parentURL.getPath()); 65 if (parentFile.exists()) { 66 parentConfiguration = true; 68 return parentURL; 69 } 70 } 71 return localURL; 72 } 73 74 public static void startup() { 75 if (isRegistered) 77 return; 78 PlatformURLHandler.register(CONFIG, PlatformURLConfigConnection.class); 79 isRegistered = true; 80 } 81 82 85 public OutputStream getOutputStream() throws IOException { 86 if (parentConfiguration || Activator.getDefault() == null || Activator.getDefault().getConfigurationLocation().isReadOnly()) 87 throw new UnknownServiceException (NLS.bind(CommonMessages.url_noOutput, url)); 88 URL resolved = getResolvedURL(); 90 if (resolved != null) { 91 String fileString = resolved.getFile(); 92 if (fileString != null) { 93 File file = new File(fileString); 94 String parent = file.getParent(); 95 if (parent != null) 96 new File(parent).mkdirs(); 97 return new FileOutputStream(file); 98 } 99 } 100 return null; 101 } 102 } 103 | Popular Tags |