001: /**
002: * Copyright Sun Microsystems, Inc. All rights reserved.
003: * The Registry class provides is used to load the native
004: * library DLL, as well as a placeholder for the top level
005: * keys, error codes, and utility methods.
006: *
007: * @version 1.0
008: *
009: */package com.sun.portal.proxylet.client.jwsdetect;
010:
011: import java.io.*;
012:
013: public class RegReader {
014: public static final int ERROR_FILE_NOT_FOUND = 2;
015: private String tempDirectory;
016: public static boolean dllLoaded = false;
017: boolean binit = false;
018: private static final RegReader THEINSTANCE = new RegReader();
019:
020: public native String getJWSVersion();
021:
022: public native String getBrowserInstallPath(String browsername,
023: String browserversion);
024:
025: static {
026: try {
027: System.out.println("trying to load library... ");
028: System.loadLibrary("RegReader");
029: System.out.println("loaded library..");
030: dllLoaded = true;
031: } catch (Throwable e) {
032: System.out
033: .println("could not load library in static block - "
034: + e.getMessage());
035: dllLoaded = false;
036: }
037: }
038:
039: private RegReader() {
040: }
041:
042: public synchronized static RegReader getInstance() {
043: if (THEINSTANCE.binit == false) {
044: try {
045: THEINSTANCE.init();
046: } catch (Exception e) {
047: // Unable to create temp directory
048: e.printStackTrace();
049: }
050: THEINSTANCE.binit = true;
051: }
052: return THEINSTANCE;
053: }
054:
055: void init() throws Exception {
056: try {
057: if (!dllLoaded) {
058: System.out.println("Loading library explicitly");
059: loadExplicitly();
060: }
061: if (tempDirectory != null) {
062: if (new File(tempDirectory).isDirectory())
063: return;
064: }
065: tempDirectory = getTempDirectory();
066: } catch (IOException e) {
067: System.out.println("Unable to create temp file");
068: throw e;
069: }
070:
071: }
072:
073: public void loadExplicitly() throws Exception {
074: System.out.println("In loadExplicitly");
075: //install dll
076: String location = extractResource("RegReader.dll");
077: System.out.println("RegReader.dll file location = " + location);
078: try {
079: System.load(location);
080: dllLoaded = true;
081: System.out
082: .println("loaded library 'RegReader.dll' explicitly");
083: } catch (Exception e) {
084: System.out.println("Exception occured in loadExplicityly");
085: dllLoaded = false;
086: throw e;
087: }
088: }
089:
090: public String extractResource(String resourceName) throws Exception {
091: String tempDirectory = getTempDirectory();
092: File registryReaderDLLFile = new File(tempDirectory
093: + resourceName);
094:
095: ClassLoader c1 = RegReader.class.getClassLoader();
096: InputStream is = c1.getResourceAsStream(resourceName);
097: if (is != null) {
098: BufferedOutputStream ostream = null;
099:
100: //imp: RegReader.dll file is extracted to the same local file by RegReader.java
101: //in both proxyletapplet.jar and jnlpclient.jar. Since, jnlpclient.jar executes
102: //after the applet, we check if it's possible to extract the resource to the file.
103: //if it's not, we assume that the file is already present. We can't just rely on
104: // the check for the file being present because the client machine might have
105: // some old dll.
106: try {
107: //generate a temporary name for the resource and write to temp file
108: ostream = new BufferedOutputStream(
109: new FileOutputStream(registryReaderDLLFile));
110: } catch (FileNotFoundException fnfe) {
111: //ignore..
112: }
113: if (ostream != null) {
114: BufferedInputStream istream = new BufferedInputStream(
115: is);
116: if (registryReaderDLLFile.exists()) {
117: registryReaderDLLFile.delete();
118: registryReaderDLLFile.createNewFile();
119: }
120:
121: System.out.println("Extracting resource "
122: + resourceName + " to directory "
123: + tempDirectory);
124:
125: int bsize = 2048;
126: int n = 0;
127: byte[] buffer = new byte[bsize];
128: while ((n = istream.read(buffer, 0, bsize)) != -1) {
129: ostream.write(buffer, 0, n);
130: }
131: istream.close();
132: ostream.close();
133: }
134: }
135: registryReaderDLLFile.deleteOnExit();
136: System.out.println("dllFilePath = "
137: + registryReaderDLLFile.getAbsolutePath());
138: System.out.println("dllFilePath = "
139: + registryReaderDLLFile.getAbsolutePath());
140: return registryReaderDLLFile.getAbsolutePath();
141: }
142:
143: public String getTempDirectory() {
144: if (tempDirectory == null || tempDirectory.trim().length() != 0) {
145: String tmpDirectory = System.getProperty("java.io.tmpdir");
146: while (true) {
147: String randomSuffix = (new Integer(
148: (int) (Math.random() * 1000))).toString();
149: tmpDirectory = System.getProperty("java.io.tmpdir")
150: + File.separator + "proxylet" + randomSuffix
151: + File.separator;
152: try {
153: File tmpDir = new File(tmpDirectory);
154: if (!tmpDir.exists()) {
155: tmpDir.mkdir();
156: break;
157: }
158: } catch (Exception e) {
159: System.out
160: .println("could not create tmp directory with proxylet suffix.. "
161: + "using default tmp dir.. "
162: + e.getMessage());
163: tmpDirectory = System.getProperty("java.io.tmpdir");
164: break;
165: }
166: }
167:
168: tempDirectory = tmpDirectory;
169: }
170:
171: return tempDirectory;
172: }
173:
174: public void setTempDirectory(String tempDir) {
175: tempDirectory = tempDir;
176: }
177:
178: public String getBrowserLaunchCommand(String browserName,
179: String browserVersion) {
180: String brName = browserName.trim().toLowerCase();
181: System.out.println("browser name " + brName);
182: System.out.println("browser version " + browserVersion);
183: String command = getBrowserInstallPath(
184: getBrowserRegistryKeyName(brName), browserVersion
185: .trim());
186:
187: System.out.println("launch url " + command);
188:
189: if (brName.indexOf("msie") != -1) {
190: command = command + "\\iexplore.exe";
191:
192: }
193: return command;
194: }
195:
196: private String getBrowserRegistryKeyName(String browserName) {
197: browserName = browserName.trim().toLowerCase();
198:
199: if (browserName.equals("mozilla"))
200: return "mozilla.org";
201: if (browserName.indexOf("firefox") != -1)
202: return "mozilla";
203: if (browserName.indexOf("netscape") != -1)
204: return "netscape";
205:
206: return browserName;
207: }
208:
209: public boolean detectJWSInstallation() {
210: String value = getJWSVersion();
211: System.out.println("detect jws installation " + value);
212: float jwsVer = 0.0f;
213: jwsVer = (new Float(value.substring(0, 3))).floatValue();
214:
215: if ((new Float(jwsVer)).compareTo(new Float(1.4)) >= 0)
216: return true;
217:
218: return false;
219: }
220: }
|