KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > PlatformURLConfigConnection


1 /*******************************************************************************
2  * Copyright (c) 2004, 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 package org.eclipse.core.internal.runtime;
12
13 import java.io.*;
14 import java.net.URL JavaDoc;
15 import java.net.UnknownServiceException JavaDoc;
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 JavaDoc FILE_PROTOCOL = "file"; //$NON-NLS-1$
23
private static boolean isRegistered = false;
24     public static final String JavaDoc CONFIG = "config"; //$NON-NLS-1$
25

26     private boolean parentConfiguration = false;
27
28     /*
29      * Constructor for the class.
30      */

31     public PlatformURLConfigConnection(URL JavaDoc url) {
32         super(url);
33     }
34
35     /* (non-Javadoc)
36      * @see org.eclipse.equinox.internal.url.PlatformURLConnection#resolve()
37      */

38     protected URL JavaDoc resolve() throws IOException {
39         String JavaDoc spec = url.getFile().trim();
40         if (spec.startsWith("/")) //$NON-NLS-1$
41
spec = spec.substring(1);
42         if (!spec.startsWith(CONFIG))
43             throw new IOException(NLS.bind(CommonMessages.url_badVariant, url.toString()));
44         String JavaDoc path = spec.substring(CONFIG.length() + 1);
45         // resolution takes parent configuration into account (if it exists)
46
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         // assume we will find the file locally
52
URL JavaDoc localURL = new URL JavaDoc(localConfig.getURL(), path);
53         if (!FILE_PROTOCOL.equals(localURL.getProtocol()) || parentConfig == null)
54             // we only support cascaded file: URLs
55
return localURL;
56         File localFile = new File(localURL.getPath());
57         if (localFile.exists())
58             // file exists in local configuration
59
return localURL;
60         // try to find in the parent configuration
61
URL JavaDoc parentURL = new URL JavaDoc(parentConfig.getURL(), path);
62         if (FILE_PROTOCOL.equals(parentURL.getProtocol())) {
63             // we only support cascaded file: URLs
64
File parentFile = new File(parentURL.getPath());
65             if (parentFile.exists()) {
66                 // parent has the location
67
parentConfiguration = true;
68                 return parentURL;
69             }
70         }
71         return localURL;
72     }
73
74     public static void startup() {
75         // register connection type for platform:/config handling
76
if (isRegistered)
77             return;
78         PlatformURLHandler.register(CONFIG, PlatformURLConfigConnection.class);
79         isRegistered = true;
80     }
81
82     /* (non-Javadoc)
83      * @see java.net.URLConnection#getOutputStream()
84      */

85     public OutputStream getOutputStream() throws IOException {
86         if (parentConfiguration || Activator.getDefault() == null || Activator.getDefault().getConfigurationLocation().isReadOnly())
87             throw new UnknownServiceException JavaDoc(NLS.bind(CommonMessages.url_noOutput, url));
88         //This is not optimal but connection is a private instance variable in the super-class.
89
URL JavaDoc resolved = getResolvedURL();
90         if (resolved != null) {
91             String JavaDoc fileString = resolved.getFile();
92             if (fileString != null) {
93                 File file = new File(fileString);
94                 String JavaDoc 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