KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > SiteURLFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.update.internal.core;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.Date JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.update.core.BaseSiteFactory;
23 import org.eclipse.update.core.ISite;
24 import org.eclipse.update.core.ISiteFactoryExtension;
25 import org.eclipse.update.core.Site;
26 import org.eclipse.update.core.SiteFeatureReferenceModel;
27 import org.eclipse.update.core.Utilities;
28 import org.eclipse.update.core.model.InvalidSiteTypeException;
29 import org.eclipse.update.core.model.SiteModelFactory;
30 import org.eclipse.update.internal.core.connection.ConnectionFactory;
31 import org.eclipse.update.internal.core.connection.IResponse;
32 import org.eclipse.update.internal.model.SiteWithTimestamp;
33
34 /**
35  * An update site factory.
36  *
37  */

38 public class SiteURLFactory extends BaseSiteFactory implements ISiteFactoryExtension {
39     
40     /*
41      * For backward compatibility.
42      */

43     public ISite createSite(URL JavaDoc url) throws CoreException, InvalidSiteTypeException {
44         return createSite(url, null);
45     }
46     /*
47      * @see ISiteFactory#createSite(URL, boolean)
48      *
49      * the URL can be of the following form
50      * 1 protocol://...../
51      * 2 protocol://.....
52      * 3 protocol://..../site.xml
53      *
54      * 1 If the file of the file of teh url ends with '/', attempt to open the stream.
55      * if it fails, add site.xml and attempt to open the stream
56      *
57      * 2 attempt to open the stream
58      * fail
59      * add '/site.xml' and attempt to open the stream
60      * sucess
61      * attempt to parse, if it fails, add '/site.xml' and attempt to open teh stream
62      *
63      * 3 open the stream
64      */

65     public ISite createSite(URL JavaDoc url, IProgressMonitor monitor) throws CoreException, InvalidSiteTypeException {
66         Site site = null;
67         InputStream JavaDoc siteStream = null;
68     
69         try {
70             SiteURLContentProvider contentProvider = new SiteURLContentProvider(url);
71     
72             URL JavaDoc resolvedURL = URLEncoder.encode(url);
73             IResponse response = ConnectionFactory.get(resolvedURL);
74             UpdateManagerUtils.checkConnectionResult(response, resolvedURL);
75             siteStream = response.getInputStream(monitor);
76             // the stream can be null if the user cancels the connection
77
if (siteStream==null) return null;
78
79             SiteModelFactory factory = this;
80             site = (Site) factory.parseSite(siteStream);
81             //System.out.println(site.getClass().getCanonicalName());
82
site.setSiteContentProvider(contentProvider);
83             contentProvider.setSite(site);
84             site.resolve(url, url);
85             site.markReadOnly();
86             /*SiteWithTimestamp siteWithTimestamp = new SiteWithTimestamp(site);
87             siteWithTimestamp.setTimestamp( new Date(response.getLastModified()));
88             site = siteWithTimestamp;*/

89             ((SiteWithTimestamp)site).setTimestamp( new Date JavaDoc(response.getLastModified()));
90         } catch (MalformedURLException JavaDoc e) {
91             throw Utilities.newCoreException(NLS.bind(Messages.SiteURLFactory_UnableToCreateURL, (new String JavaDoc[] { url == null ? "" : url.toExternalForm() })), e); //$NON-NLS-1$
92
} catch (IOException JavaDoc e) {
93             throw Utilities.newCoreException(NLS.bind(Messages.SiteURLFactory_UnableToAccessSiteStream, (new String JavaDoc[] { url == null ? "" : url.toExternalForm() })), ISite.SITE_ACCESS_EXCEPTION, e); //$NON-NLS-1$
94
} finally {
95             if (siteStream != null) {
96                 try {
97                     siteStream.close();
98                 } catch (IOException JavaDoc e) {
99                 }
100             }
101         }
102         return site;
103     }
104
105     /*
106      * @see SiteModelFactory#canParseSiteType(String)
107      */

108     public boolean canParseSiteType(String JavaDoc type) {
109         return (super.canParseSiteType(type) || SiteURLContentProvider.SITE_TYPE.equalsIgnoreCase(type));
110     }
111     /* (non-Javadoc)
112      * @see org.eclipse.update.core.BaseSiteFactory#createFeatureReferenceModel()
113      */

114     public SiteFeatureReferenceModel createFeatureReferenceModel() {
115         return new UpdateSiteFeatureReference();
116     }
117
118 }
119
Popular Tags