KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.internal.boot.PlatformURLConnection;
16 import org.eclipse.core.internal.boot.PlatformURLHandler;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.osgi.util.NLS;
19 import org.osgi.framework.Bundle;
20
21 public class PlatformURLMetaConnection extends PlatformURLConnection {
22     private Bundle target = null;
23     private static boolean isRegistered = false;
24     public static final String JavaDoc META = "meta"; //$NON-NLS-1$
25

26     /*
27      * Constructor for the class.
28      */

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

36     protected URL JavaDoc resolve() throws IOException {
37         String JavaDoc spec = url.getFile().trim();
38         if (spec.startsWith("/")) //$NON-NLS-1$
39
spec = spec.substring(1);
40         if (!spec.startsWith(META))
41             throw new IOException(NLS.bind(CommonMessages.url_badVariant, url.toString()));
42         int ix = spec.indexOf("/", META.length() + 1); //$NON-NLS-1$
43
String JavaDoc ref = ix == -1 ? spec.substring(META.length() + 1) : spec.substring(META.length() + 1, ix);
44         String JavaDoc id = getId(ref);
45         Activator activator = Activator.getDefault();
46         if (activator == null)
47             throw new IOException(CommonMessages.activator_not_available);
48         target = activator.getBundle(id);
49         if (target == null)
50             throw new IOException(NLS.bind(CommonMessages.url_resolvePlugin, url.toString()));
51         IPath path = MetaDataKeeper.getMetaArea().getStateLocation(target);
52         if (ix != -1 || (ix + 1) <= spec.length())
53             path = path.append(spec.substring(ix + 1));
54         return path.toFile().toURL();
55     }
56
57     public static void startup() {
58         // register connection type for platform:/meta handling
59
if (isRegistered)
60             return;
61         PlatformURLHandler.register(META, PlatformURLMetaConnection.class);
62         isRegistered = true;
63     }
64
65     /* (non-Javadoc)
66      * @see java.net.URLConnection#getOutputStream()
67      */

68     public OutputStream getOutputStream() throws IOException {
69         //This is not optimal but connection is a private instance variable in super.
70
URL JavaDoc resolved = getResolvedURL();
71         if (resolved != null) {
72             String JavaDoc fileString = resolved.getFile();
73             if (fileString != null) {
74                 File file = new File(fileString);
75                 String JavaDoc parent = file.getParent();
76                 if (parent != null)
77                     new File(parent).mkdirs();
78                 return new FileOutputStream(file);
79             }
80         }
81         return null;
82     }
83 }
84
Popular Tags