Skip to main content
summaryrefslogtreecommitdiffstats
blob: a46aa59444db8cd6a728353438b51648273016a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*******************************************************************************
 * Copyright (c) 2001, 2007 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.core.internal.jsflibraryregistry.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;

/**
 * Utility for checking for, and upgrading the JSF Library Registry when the EMF model changes.
 * @deprecated
 */
public class JSFLibraryRegistryUpgradeUtil {	
	static String v1Tov2UpgradeURL = "http://www.eclipse.org/JSFxxxxxxxxxxxxxxxxxxxxxx"; //$NON-NLS-1$
	
	/**
	 * default state is OK, no upgrade
	 */
	private UpgradeStatus	upgradeStatus;

	/**
	 * The workspace-relative part of the URL of the JSF Library Registry 
	 * persistence store. (version 1)
	 */
	public static final String JSF_LIBRARY_REGISTRY_V1_URL = ".metadata/.plugins/org.eclipse.jst.jsf.core/JSFLibraryRegistry.xml"; //$NON-NLS-1$

	/**
	 * The workspace-relative part of the URL of the JSF Library Registry 
	 * persistence store. (version 2)
	 */
	public static final String JSF_LIBRARY_REGISTRY_V2_URL = ".metadata/.plugins/org.eclipse.jst.jsf.core/JSFLibraryRegistryV2.xml"; //$NON-NLS-1$

	/**
	 * The LATEST VERSION of the workspace-relative part of the URL of the JSF Library Registry 
	 * persistence store. 
	 */
	public static final String JSF_LIBRARY_REGISTRY_LATESTVERSION_URL = JSF_LIBRARY_REGISTRY_V2_URL;
	/**
	 * The latest version value.
	 */
	public static final int	   LATESTVERSION = 2;

	private static final int NO_VERSION = 0;
	
	private static JSFLibraryRegistryUpgradeUtil INSTANCE;
		
	/**
	 * @return the stateful runtime singleton
	 */
	public static synchronized JSFLibraryRegistryUpgradeUtil getInstance(){
		if (INSTANCE == null){
			INSTANCE = new JSFLibraryRegistryUpgradeUtil();
		}
		return INSTANCE;
	}
	
	/**
	 * Return the URI for the specified JSF Library Registry
	 * @param registryVersion
	 * @return URI 
	 * @throws MalformedURLException
	 */
	public static URI getRegistryURI(String registryVersion) throws MalformedURLException {
		URL jsfLibRegURL = new URL(Platform.getInstanceLocation().getURL(), registryVersion);
		return URI.createURI(jsfLibRegURL.toString());
	}
	
	/**
	 * @param originalFile
	 * @return the backup file name for a file
	 */
	public static String getBackupFileName(final String originalFile)
	{
	    return originalFile.concat(".bkp"); //$NON-NLS-1$
	}
	
	/**
	 * Upgrades the JSF Library registry from oldest to newest
	 * @param expectedVersion 
	 */
	public void upgradeRegistryIfNecessary(int expectedVersion) {
		//when adding upgrades, the upgrades should progress from oldest to newest
		try
		{
			int curVersion = getCurVersion();
			
			if (curVersion < expectedVersion && curVersion != NO_VERSION)
			{
				UpgradeOperation op = getUpgradeOperation(curVersion);
				
				if (op.canExecute())
				{
					try
					{
						// TODO: when move to Java 5, use co-variant return
						upgradeStatus = (UpgradeStatus)
							op.execute(new NullProgressMonitor(), null);
						upgradeStatus.setUpgradeOperation(op);
					}
					catch (ExecutionException e)
					{
						// should never happen since we control the URL's
						JSFCorePlugin.log(IStatus.ERROR, "Error during loading JSF Library registry", e); //$NON-NLS-1$
						//TODO: flag failure in status
						upgradeStatus = 
							new UpgradeStatus(IStatus.ERROR, true, Messages.JSFLibraryRegistryUpgradeUtil_Error); 
					}
				}
				else
				{
    				// TODO: what if can't execute?
                    upgradeStatus = 
                        new UpgradeStatus(IStatus.ERROR, false, Messages.JSFLibraryRegistryUpgradeUtil_Error); 				}
			}
			else
			{
			    // everything ok, not upgrade
			    upgradeStatus = new UpgradeStatus();
			}
		}
	    catch (MalformedURLException e) {	
			// should never happen since we control the URL's
			JSFCorePlugin.log(IStatus.ERROR, "Error during loading JSF Library registry", e); //$NON-NLS-1$
		}
	}

	private int getCurVersion() throws MalformedURLException
	{
		// TODO: need generalized algorithm here
		URI v2File = getRegistryURI(JSF_LIBRARY_REGISTRY_V2_URL);
		File file = new File(v2File.toFileString());
		if (file.exists())
		{
			return 2;
		}
		
		URI v1File = getRegistryURI(JSF_LIBRARY_REGISTRY_V1_URL);
		file = new File(v1File.toFileString());
		if (file.exists())
		{
			return 1;
		}
		return NO_VERSION;
	}
	
	/**
	 * @param curVersion
	 * @return the upgrade operation to move from curVersion to the latest
	 * @throws MalformedURLException
	 */
	protected UpgradeOperation getUpgradeOperation(int curVersion) throws MalformedURLException
	{
		UpgradeOperation  op = new UpgradeOperation(Messages.JSFLibraryRegistryUpgradeUtil_Operation); 
		switch(curVersion)
		{
			case 1:
				op.addVersionUpgrade(
						new MigrateV1toV2Operation(Messages.JSFLibraryRegistryUpgradeUtil_v1Tov2Operation 
								,getRegistryURI(JSF_LIBRARY_REGISTRY_V1_URL)
								, getRegistryURI(JSF_LIBRARY_REGISTRY_V2_URL)));
		}
		
		return op;
	}
	
	
	/**
	 * @return array of {@link UpgradeStatus}s.  There can be more than one if the registry has been upgraded by more than one version.
	 * Will not be null.   
	 */
	public UpgradeStatus getUpgradeStatus(){
		return this.upgradeStatus;
	}

	static void deleteFile(String fileName) {
		File f = new File(fileName);
		if (f.exists()){
			f.delete();
			if (f.exists())
				f.deleteOnExit();
		}
	}

	static void copyFile(String srcFileName, String destFileName) {
	
		File srcFile = new File(srcFileName);
		File destFile = new File(destFileName);
		FileInputStream from = null;
		FileOutputStream to = null;
		try {
			from = new FileInputStream(srcFile);
			to = new FileOutputStream(destFile);
			byte[] buffer = new byte[4096];
			int bytesRead;

			while ((bytesRead = from.read(buffer)) != -1)
				to.write(buffer, 0, bytesRead); // write
		} catch (IOException ioe){
			JSFCorePlugin.log(ioe, "Error during file copy"); //$NON-NLS-1$
		} finally {
			if (from != null)
				try {
					from.close();
				} catch (IOException e) {
		            JSFCorePlugin.log(e, "Error during file close"); //$NON-NLS-1$
				}
			if (to != null)
				try {
					to.close();
				} catch (IOException e) {
                    JSFCorePlugin.log(e, "Error during file close"); //$NON-NLS-1$
				}
		}

	}

}

Back to the top