KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 import java.io.*;
13 import java.net.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.update.core.*;
18
19 /**
20  * Plugin Content Consumer on a Site
21  * for a plugin that will run from a jar
22  */

23 public class SiteFilePackedPluginContentConsumer extends ContentConsumer {
24
25     private IPluginEntry pluginEntry;
26     private ISite site;
27     private boolean closed = false;
28     private String JavaDoc jarPath;
29     private String JavaDoc tempPath;
30
31     /*
32      * Constructor
33      */

34     public SiteFilePackedPluginContentConsumer(IPluginEntry pluginEntry, ISite site) {
35         this.pluginEntry = pluginEntry;
36         this.site = site;
37     }
38
39     /*
40      * @see ISiteContentConsumer#store(ContentReference, IProgressMonitor)
41      */

42     public void store(ContentReference contentReference, IProgressMonitor monitor) throws CoreException {
43         InputStream inStream = null;
44
45         if (closed) {
46             UpdateCore.warn("Attempt to store in a closed SiteFilePluginContentConsumer", new Exception JavaDoc()); //$NON-NLS-1$
47
return;
48         }
49
50         try {
51             URL newURL = new URL(site.getURL(), Site.DEFAULT_PLUGIN_PATH + pluginEntry.getVersionedIdentifier().toString() + ".jar"); //$NON-NLS-1$
52
inStream = contentReference.getInputStream();
53             jarPath = newURL.getFile().replace(File.separatorChar, '/');
54             File jarFile = new File(jarPath);
55             if (jarFile.exists()) {
56                 throw Utilities.newCoreException(NLS.bind(Messages.UpdateManagerUtils_FileAlreadyExists, (new Object JavaDoc[] { jarFile })), null);
57             }
58             // error recovery
59
tempPath= ErrorRecoveryLog.getLocalRandomIdentifier(jarPath+".tmp"); //$NON-NLS-1$
60
ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.BUNDLE_JAR_ENTRY, tempPath);
61             //
62
UpdateManagerUtils.copyToLocal(inStream, tempPath, null);
63         } catch (IOException e) {
64             throw Utilities.newCoreException(NLS.bind(Messages.GlobalConsumer_ErrorCreatingFile, (new String JavaDoc[] { tempPath })), e);
65         } finally {
66             if (inStream != null) {
67                 try {
68                     // close stream
69
inStream.close();
70                 } catch (IOException e) {
71                 }
72             }
73         }
74     }
75
76     /*
77      * @see ISiteContentConsumer#close()
78      */

79     public void close() throws CoreException {
80
81         if (closed) {
82             UpdateCore.warn("Attempt to close a closed SiteFilePluginContentConsumer", new Exception JavaDoc()); //$NON-NLS-1$
83
return;
84         }
85
86         if (tempPath != null) {
87             // rename file
88
ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.RENAME_ENTRY, tempPath);
89             File fileToRename = new File(tempPath);
90             boolean sucess = false;
91             if (fileToRename.exists()) {
92                 File renamedFile = new File(jarPath);
93                 sucess = fileToRename.renameTo(renamedFile);
94             }
95             if (!sucess) {
96                 String JavaDoc msg = NLS.bind(Messages.ContentConsumer_UnableToRename, (new String JavaDoc[] { tempPath, jarPath }));
97                 throw Utilities.newCoreException(msg, new Exception JavaDoc(msg));
98             }
99         }
100
101         if (site instanceof SiteFile)
102              ((SiteFile) site).addPluginEntry(pluginEntry);
103         closed = true;
104     }
105
106     /*
107      *
108      */

109     public void abort() throws CoreException {
110
111         if (closed) {
112             UpdateCore.warn("Attempt to abort a closed SiteFilePluginContentConsumer", new Exception JavaDoc()); //$NON-NLS-1$
113
return;
114         }
115
116         boolean sucess = true;
117
118         // delete plugin.jar
119
if (jarPath != null) {
120             ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.DELETE_ENTRY, jarPath);
121             File fileToRemove = new File(jarPath);
122
123             if (fileToRemove.exists()) {
124                 sucess = fileToRemove.delete();
125             }
126         }
127
128         if (!sucess) {
129             String JavaDoc msg = NLS.bind(Messages.SiteFilePackedPluginContentConsumer_unableToDelete, (new String JavaDoc[] { jarPath }));
130             UpdateCore.log(msg, null);
131         }
132         closed = true;
133     }
134
135 }
136
Popular Tags