Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2010 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
|
42 |
package org.netbeans.modules.derby; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.util.logging.Level; |
47 |
import java.util.logging.Logger; |
48 |
import org.openide.filesystems.FileObject; |
49 |
import org.openide.filesystems.FileUtil; |
50 |
|
51 |
/** |
52 |
* Registers installation of JavaDB by creating instance file in cluster config |
53 |
* directory. Designed to be called from installer. |
54 |
* The DerbyRegistration can called multiple times but only the first invocation |
55 |
* is stored. |
56 |
* <p> |
57 |
* Sample command line<br> |
58 |
* |
59 |
* java -cp platform/core/core.jar:platform/lib/boot.jar:platform/lib/org-openide-modules.jar: |
60 |
* platform/core/org-openide-filesystems.jar:platform/lib/org-openide-util.jar: |
61 |
* platform/lib/org-openide-util-lookup.jar:ide/modules/org-netbeans-modules-derby.jar |
62 |
* org.netbeans.modules.derby.DerbyRegistration nb /usr/local/share/java/jdk1.6.0_18/db |
63 |
* |
64 |
* @author Jiri Rechtacek |
65 |
* @see #main(args) |
66 |
*/ |
67 |
public class DerbyRegistration { |
68 |
|
69 |
private static final String DERBY_REGISTRATION_DIR = "JavaDB"; // NOI18N |
70 |
private static final String DERBY_REGISTRATION_FILE = "registration_instance"; // NOI18N |
71 |
private static final String ATTR_DERBY_HOME = "javadb_installation_home"; // NOI18N |
72 |
private static final Logger LOGGER = Logger.getLogger(DerbyRegistration.class.getName()); |
73 |
|
74 |
/** |
75 |
* Performs registration. |
76 |
* |
77 |
* Exit codes:<p> |
78 |
* <ul> |
79 |
* <li> 2: could not find/create config/JavaDB folder |
80 |
* <li> 3: could not find JavaDB installation home |
81 |
* <li> 4: JavaDB installation doesn't contain expected libraries, i.e. lib/derbyclient.jar |
82 |
* <li> 5: could not write registration FileObject |
83 |
* </ul> |
84 |
* @param args command line arguments - cluster path and Derby home expected |
85 |
*/ |
86 |
public static void main(String[] args) throws IOException { |
87 |
if (args.length < 2) { |
88 |
System.out.println("Parameters: <nb clusterDir> <Derby home>"); |
89 |
System.exit(-1); |
90 |
} |
91 |
|
92 |
int status = registerDerbyInstallation(args[0], args[1]); |
93 |
System.exit(status); |
94 |
} |
95 |
|
96 |
private static int registerDerbyInstallation(String cluster, String derbyHome) throws IOException { |
97 |
LOGGER.log(Level.FINE, "Cluster for JavaDB registration is " + cluster); |
98 |
System.setProperty("netbeans.user", cluster); // NOI18N |
99 |
// check writable cluster |
100 |
FileObject javaDBRegistrationDir = FileUtil.getConfigFile(DERBY_REGISTRATION_DIR); |
101 |
|
102 |
if (javaDBRegistrationDir == null) { |
103 |
javaDBRegistrationDir = FileUtil.createFolder(FileUtil.getConfigRoot(), DERBY_REGISTRATION_DIR); |
104 |
if (javaDBRegistrationDir == null) { |
105 |
LOGGER.log(Level.INFO, "Cannot register the default JavaDB. The config/" + DERBY_REGISTRATION_DIR + " folder cannot be created."); // NOI18N |
106 |
return 2; |
107 |
} |
108 |
} |
109 |
|
110 |
// check if exists |
111 |
File derbyHomeFile = new File(derbyHome); |
112 |
if (!derbyHomeFile.exists()) { |
113 |
LOGGER.log(Level.INFO, "Cannot register the default JavaDB. " // NOI18N |
114 |
+ "The JavaDB installation directory " + derbyHome // NOI18N |
115 |
+ " does not exist."); // NOI18N |
116 |
return 3; |
117 |
} |
118 |
|
119 |
// check if solid JavaDB home |
120 |
if (! Util.isDerbyInstallLocation(derbyHomeFile)) { |
121 |
LOGGER.log(Level.INFO, "Cannot register the default JavaDB. " // NOI18N |
122 |
+ "The JavaDB installation directory " + derbyHome // NOI18N |
123 |
+ " does not contain expected libraries."); // NOI18N |
124 |
return 4; |
125 |
} |
126 |
|
127 |
FileObject registrationFO = FileUtil.getConfigFile(DERBY_REGISTRATION_DIR + "/" + DERBY_REGISTRATION_FILE); // NOI18N |
128 |
if (registrationFO == null) { |
129 |
try { |
130 |
registrationFO = javaDBRegistrationDir.createData(DERBY_REGISTRATION_FILE); |
131 |
registrationFO.setAttribute(ATTR_DERBY_HOME, derbyHome); |
132 |
LOGGER.log(Level.FINE, "New registration links to " + registrationFO.getAttribute(ATTR_DERBY_HOME)); // NOI18N |
133 |
} catch (IOException e) { |
134 |
LOGGER.log(Level.INFO, "Cannot register JavaDB, cause " + e.getLocalizedMessage(), e); // NOI18N |
135 |
return 5; |
136 |
} |
137 |
} else { |
138 |
LOGGER.log(Level.INFO, "The previous registration found. Links to " + registrationFO.getAttribute(ATTR_DERBY_HOME)); // NOI18N |
139 |
} |
140 |
|
141 |
return 0; |
142 |
} |
143 |
|
144 |
public static String getRegisteredDerbyHome () { |
145 |
FileObject registrationFO = FileUtil.getConfigFile(DERBY_REGISTRATION_DIR + "/" + DERBY_REGISTRATION_FILE); // NOI18N |
146 |
String home = (String) (registrationFO == null ? null : registrationFO.getAttribute(ATTR_DERBY_HOME)); |
147 |
LOGGER.log(Level.FINE, "Registered home is " + home); |
148 |
return home; |
149 |
} |
150 |
|
151 |
} |