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.common;
010:
011: import com.sun.portal.proxylet.client.common.ui.ProxyletUI;
012: import com.sun.portal.proxylet.client.common.browser.BrowserHelper;
013: import java.io.*;
014:
015: public class RegReader {
016: public static final int ERROR_FILE_NOT_FOUND = 2;
017: private String tempDirectory;
018: public static boolean dllLoaded = false;
019: boolean binit = false;
020: private static final RegReader THEINSTANCE = new RegReader();
021:
022: public native String getJWSVersion();
023:
024: public native String getBrowserInstallPath(String browsername,
025: String browserversion);
026:
027: static {
028: try {
029: Log.debugu("trying to load library... ");
030: System.loadLibrary("RegReader");
031: Log.debugu("loaded library..");
032: dllLoaded = true;
033: } catch (Throwable e) {
034: e.printStackTrace();
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: String location = null;
050: location = BrowserHelper
051: .getNewfileLocation(ProxyletUI.frame);
052: THEINSTANCE.setTempDirectory(location);
053: }
054: THEINSTANCE.binit = true;
055:
056: }
057: return THEINSTANCE;
058: }
059:
060: void init() throws Exception {
061: try {
062: if (!dllLoaded) {
063: Log.debug("Loading library explicitly");
064: loadExplicitly();
065: }
066: if (tempDirectory != null) {
067: if (new File(tempDirectory).isDirectory())
068: return;
069: }
070: File f = File.createTempFile("abcdd", ".sunxyz");
071: tempDirectory = f.getPath();
072: tempDirectory = tempDirectory.substring(0, tempDirectory
073: .lastIndexOf("\\") + 1);
074: Log.debugu(" tempDirectory = " + tempDirectory);
075: f.delete();
076: } catch (IOException e) {
077: System.out.println("Unable to create temp file");
078: ProxyletUI.setText(Param.getString("perr.8",
079: "ERR: Unable to write to disk"));
080: throw e;
081: }
082:
083: }
084:
085: public void loadExplicitly() throws Exception {
086: System.out.println("In loadExplicityly");
087: //install dll
088: String location = extractResource("RegReader.dll");
089: Log.debugu("RegReader.dll file location = " + location);
090: System.out.println("RegReader.dll file location = " + location);
091: try {
092: System.load(location);
093: dllLoaded = true;
094: Log.debugu("loaded library 'RegReader.dll' explicitly");
095: System.out
096: .println("loaded library 'RegReader.dll' explicitly");
097: } catch (Exception e) {
098: System.out.println("Exception occured in loadExplicityly");
099: dllLoaded = false;
100: throw e;
101: }
102: }
103:
104: public String extractResource(String resourceName) throws Exception {
105: String tempDirectory = getTempDirectory();
106: File registryReaderDLLFile = new File(tempDirectory
107: + resourceName);
108:
109: ClassLoader c1 = RegReader.class.getClassLoader();
110: InputStream is = c1.getResourceAsStream(resourceName);
111: if (is != null) {
112: BufferedOutputStream ostream = null;
113:
114: //imp: RegReader.dll file is extracted to the same local file by RegReader.java
115: //in both proxyletapplet.jar and jnlpclient.jar. Since, jnlpclient.jar executes
116: //after the applet, we check if it's possible to extract the resource to the file.
117: //if it's not, we assume that the file is already present. We can't just rely on
118: // the check for the file being present because the client machine might have
119: // some old dll.
120: try {
121: //generate a temporary name for the resource and write to temp file
122: ostream = new BufferedOutputStream(
123: new FileOutputStream(registryReaderDLLFile));
124: } catch (FileNotFoundException fnfe) {
125: //ignore..
126: }
127: if (ostream != null) {
128: BufferedInputStream istream = new BufferedInputStream(
129: is);
130: if (registryReaderDLLFile.exists()) {
131: registryReaderDLLFile.delete();
132: registryReaderDLLFile.createNewFile();
133: }
134:
135: Log.debugu("Extracting resource " + resourceName
136: + " to directory " + tempDirectory);
137:
138: int bsize = 2048;
139: int n = 0;
140: byte[] buffer = new byte[bsize];
141: while ((n = istream.read(buffer, 0, bsize)) != -1) {
142: ostream.write(buffer, 0, n);
143: }
144: istream.close();
145: ostream.close();
146: }
147: }
148: registryReaderDLLFile.deleteOnExit();
149: Log.debugu("dllFilePath = "
150: + registryReaderDLLFile.getAbsolutePath());
151: System.out.println("dllFilePath = "
152: + registryReaderDLLFile.getAbsolutePath());
153: return registryReaderDLLFile.getAbsolutePath();
154: }
155:
156: public String getTempDirectory() {
157: if (tempDirectory == null || tempDirectory.trim().length() != 0) {
158: try {
159: File f = File.createTempFile("abcdd", ".sunxyz");
160: tempDirectory = f.getPath();
161: tempDirectory = tempDirectory.substring(0,
162: tempDirectory.lastIndexOf("\\") + 1);
163: f.delete();
164: } catch (IOException e) {
165: ProxyletUI.setText(Param.getString("perr.2",
166: "ERR: installing dll."));
167: e.printStackTrace();
168: }
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: System.out.println("Inside getBrowserlaunchcommand");
181: String brName = browserName.trim().toLowerCase();
182: System.out.println("browser name " + brName);
183: System.out.println("browser version " + browserVersion);
184: String command = getBrowserInstallPath(
185: getBrowserRegistryKeyName(brName), browserVersion
186: .trim());
187:
188: System.out.println("launch url " + command);
189:
190: if (brName.indexOf("msie") != -1) {
191: command = command + "\\iexplore.exe";
192:
193: }
194: return command;
195: }
196:
197: private String getBrowserRegistryKeyName(String browserName) {
198: browserName = browserName.trim().toLowerCase();
199:
200: if (browserName.equals("mozilla"))
201: return "mozilla.org";
202: if (browserName.indexOf("firefox") != -1)
203: return "mozilla";
204: if (browserName.indexOf("netscape") != -1)
205: return "netscape";
206:
207: return browserName;
208: }
209:
210: public boolean detectJWSInstallation() {
211: String value = getJWSVersion();
212: System.out.println("detect jws installation " + value);
213: float jwsVer = 0.0f;
214: jwsVer = (new Float(value.substring(0, 3))).floatValue();
215:
216: if ((new Float(jwsVer)).compareTo(new Float(1.4)) >= 0)
217: return true;
218:
219: return false;
220: }
221:
222: }
|