001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * ImportDataSource.java
043: *
044: * Created on June 17, 2005, 2:49 PM
045: * Modified on June 13, 2007
046: *
047: */
048:
049: package org.netbeans.modules.visualweb.dataconnectivity.utils;
050:
051: import java.awt.Dialog;
052: import java.io.File;
053: import java.io.FileInputStream;
054: import java.io.FileOutputStream;
055: import java.io.IOException;
056: import java.io.InputStream;
057: import java.io.OutputStream;
058: import javax.swing.JButton;
059: import javax.swing.SwingUtilities;
060: import org.netbeans.api.project.Project;
061: import org.netbeans.api.project.Project;
062: import org.netbeans.modules.visualweb.dataconnectivity.naming.DatabaseSettingsImporter;
063: import org.netbeans.modules.visualweb.dataconnectivity.ui.DatasourceUISettings;
064: import org.netbeans.modules.visualweb.dataconnectivity.ui.MissingConnectionsAlertPanel;
065: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
066: import org.openide.DialogDescriptor;
067: import org.openide.DialogDisplayer;
068: import org.openide.modules.InstalledFileLocator;
069: import org.openide.util.NbBundle;
070:
071: /**
072: * This is a utility class that has methods to migrate the old but critical
073: * Creator 2u1 settings to NB 6.0 so that Creator projects can be opened in
074: * NB 6.0 smoothly (without unresolved references).
075: *
076: * @author cnguyencasj, jbaker
077: */
078:
079: public class ImportDataSource {
080:
081: private static File curImportDir = null;
082: private static String[] srcPaths = new String[] {
083: ".Creator/2_0/context.xml", ".Creator/2_0/jdbc-drivers",
084: ".Creator/2_1/context.xml", ".Creator/2_1/jdbc-drivers",
085: ".netbeans/5.5/context.xml", ".netbeans/5.5/jdbc-drivers",
086: ".netbeans/5.5.1/context.xml",
087: ".netbeans/5.5.1/jdbc-drivers" }; //NOI18N
088: private static String[] destPaths = new String[] {
089: "migrated/2_0/context.xml", "jdbc-drivers",
090: "migrated/2_1/context.xml", "jdbc-drivers",
091: "migrated/5_5/context.xml", "jdbc-drivers",
092: "migrated/5_5_1/context.xml", "jdbc-drivers" }; //NOI18N
093: private static File currentUserdir = getCurrentUserdir();
094: private static final int CREATOR2 = 0;
095: private static final int CREATOR2U1 = 2;
096: private static final int VWP55 = 4;
097: private static final int VWP551 = 6;
098: private static final String config = "config"; //NOI18N
099:
100: /** Last time in ms when the Broken References alert was shown. */
101: private static long brokenAlertLastTime = 0;
102:
103: /** Is Broken References alert shown now? */
104: private static boolean brokenAlertShown = false;
105:
106: /** Timeout within which request to show alert will be ignored. */
107: private static int BROKEN_ALERT_TIMEOUT = 1000;
108:
109: public ImportDataSource() {
110: // Need to detect what version of the project and initialize curImportDir
111: }
112:
113: private static File getCurrentUserdir() {
114: if (currentUserdir == null) {
115: currentUserdir = InstalledFileLocator.getDefault().locate(
116: "config", null, false).getParentFile(); //NOI18N
117: }
118:
119: return currentUserdir;
120: }
121:
122: public static boolean isMigrated() {
123: File migratedDir = new File(currentUserdir.getAbsolutePath()
124: + File.separator + config + File.separator + "migrated"); //NOI18N
125: if (migratedDir != null && migratedDir.exists()
126: && migratedDir.isDirectory()) {
127: return true;
128: }
129: return false;
130: }
131:
132: public static boolean isMigrated(String path) {
133: File file = new File(currentUserdir.getAbsolutePath()
134: + File.separator + path);
135: return (file.exists());
136: }
137:
138: public static boolean isMigrated(int release) {
139: File file = new File(currentUserdir.getAbsolutePath()
140: + File.separator + config + File.separator
141: + destPaths[release]);
142: return (file.exists());
143: }
144:
145: public static void prepareCopy() throws IOException {
146: // Check if already migrated
147: if (isMigrated()) {
148: return;
149: }
150:
151: // User Home Dir
152: String userHome = System.getProperty("user.home"); //NOI18N
153: if (userHome == null) {
154: // cannot locate the old user dirs if this is null
155: return;
156: }
157:
158: // Get current NB user dir
159: File nbUserDir = getCurrentUserdir();
160:
161: // For each element in the srcPaths array, perform the copy
162: // to the corresponding destPaths element
163: for (int i = 0; i < srcPaths.length; i++) {
164: File src = new File(userHome + File.separator + srcPaths[i]);
165: File dest;
166: if (destPaths[i].equals("jdbc-drivers")) { //NOI18N
167: dest = new File(nbUserDir.getAbsolutePath()
168: + File.separator + destPaths[i]);
169: } else {
170: dest = new File(nbUserDir.getAbsolutePath()
171: + File.separator + config + File.separator
172: + destPaths[i]);
173: }
174: System.out.println("src [" + i + "]:"
175: + src.getAbsolutePath()); //NOI18N
176: System.out.println("dest [" + i + "]:"
177: + dest.getAbsolutePath()); //NOI18N
178:
179: // For performance reason, if context.xml dest file already exists, skip
180: // However, this should never happen now that we put under migrated dir
181: if (dest.exists()
182: && (i == CREATOR2 || i == CREATOR2U1 || i == VWP55 || i == VWP551)) {
183: i++;
184: System.out.println("this should never happen now"); //NOI18N
185: } else if (src.exists()) {
186:
187: // Check parent directory of dest if not exist, create
188: File parent = dest.getParentFile();
189: if (!parent.exists()) {
190: parent.mkdirs();
191: }
192: if (src.isDirectory()) {
193: copyDirectory(src, dest);
194: } else if (src.isFile()) {
195: boolean append = false;
196: if (dest.getName().equals("build.properties")) { //NOI18N
197: append = true;
198: }
199: copy(src, dest, append);
200: }
201: }
202: }
203: }
204:
205: public static void copyDirectory(File srcDir, File dstDir)
206: throws IOException {
207: if (srcDir.isDirectory()) {
208: if (!dstDir.exists()) {
209: dstDir.mkdir();
210: }
211: String[] children = srcDir.list();
212: for (int i = 0; i < children.length; i++) {
213: copyDirectory(new File(srcDir, children[i]), new File(
214: dstDir, children[i]));
215: }
216: } else {
217: copy(srcDir, dstDir, false);
218: }
219: }
220:
221: public static void copy(File src, File dst, boolean append)
222: throws IOException {
223: InputStream in = new FileInputStream(src);
224:
225: OutputStream out = new FileOutputStream(dst, append);
226:
227: // Transfer bytes from in to out
228: byte[] buf = new byte[1024];
229: int len;
230: while ((len = in.read(buf)) > 0) {
231: out.write(buf, 0, len);
232: }
233: in.close();
234: out.close();
235: }
236:
237: /**
238: * Determine if project is a legacy (Creator 2, Creator 2, NB 5.5, NB 5.5.1 project) and
239: * @param project
240: * @return return true if project is a legacy project
241: */
242: public static boolean isLegacyProject(Project project) {
243: boolean isLegacyProject = false;
244:
245: // if project is a visualweb or creator project then find out whether it is a Creator 2 project
246: if (JsfProjectUtils.getProjectVersion(project).equals("2.0")
247: || JsfProjectUtils.getProjectVersion(project).equals(
248: "3.0")) { //NOI18N
249: isLegacyProject = true;
250: }
251:
252: return isLegacyProject;
253: }
254:
255: /**
256: * Show alert message box informing user that a project has missing
257: * database connections. This method can be safely called from any thread, e.g. during
258: * the project opening, and it will take care about showing message box only
259: * once for several subsequent calls during a timeout.
260: * The alert box has also "show this warning again" check box.
261: */
262: public static synchronized void showAlert() {
263: // Do not show alert if it is already shown or if it was shown
264: // in last BROKEN_ALERT_TIMEOUT milliseconds or if user do not wish it.
265: if (brokenAlertShown
266: || brokenAlertLastTime + BROKEN_ALERT_TIMEOUT > System
267: .currentTimeMillis()
268: || !DatasourceUISettings.getDefault()
269: .isShowAgainBrokenDatasourceAlert()) {
270: return;
271: }
272:
273: if (brokenAlertShown
274: || brokenAlertLastTime + BROKEN_ALERT_TIMEOUT > System
275: .currentTimeMillis()) {
276: return;
277: }
278:
279: brokenAlertShown = true;
280: SwingUtilities.invokeLater(new Runnable() {
281: public void run() {
282: try {
283: MissingConnectionsAlertPanel alert = new MissingConnectionsAlertPanel();
284: JButton close = new JButton(NbBundle.getMessage(
285: MissingConnectionsAlertPanel.class,
286: "LBL_UpdateDatasourcesCustomizer_Close")); //NOI18N
287: close
288: .getAccessibleContext()
289: .setAccessibleDescription(
290: NbBundle
291: .getMessage(
292: MissingConnectionsAlertPanel.class,
293: "ACSD_UpdateDatasourcesCustomizer_Close")); //NOI18N
294: DialogDescriptor dd = new DialogDescriptor(
295: alert,
296: NbBundle.getMessage(
297: MissingConnectionsAlertPanel.class,
298: "MSG_Update_Datasources_Title"), //NOI18N
299: true, new Object[] { close }, close,
300: DialogDescriptor.DEFAULT_ALIGN, null, null);
301: dd.setMessageType(DialogDescriptor.WARNING_MESSAGE); //NOI18N
302: Dialog dlg = DialogDisplayer.getDefault()
303: .createDialog(dd);
304: dlg.setVisible(true);
305: } finally {
306: synchronized (MissingConnectionsAlertPanel.class) {
307: brokenAlertLastTime = System
308: .currentTimeMillis();
309: brokenAlertShown = false;
310: }
311: }
312: }
313: });
314: }
315:
316: }
|