KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > converter > bea > Weblogic81Utils


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.converter.bea;
18
19 import java.util.regex.Pattern JavaDoc;
20 import java.util.regex.Matcher JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.BufferedReader JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.net.URLClassLoader JavaDoc;
35 import java.net.URL JavaDoc;
36
37 /**
38  * Reads information out of the WebLogic domain directory.
39  * Needs access to the WebLogic JARs in the weblogic81/server/lib directory.
40  *
41  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
42  */

43 public class Weblogic81Utils {
44     private final static Pattern JavaDoc ENCRYPTED_STRING = Pattern.compile("\\\"\\{\\S+\\}\\S+?\\\"");
45     private Object JavaDoc decoder;
46     private Method JavaDoc decode;
47     private Object JavaDoc decrypter;
48     private Method JavaDoc decrypt;
49     private File JavaDoc domainDir;
50
51     public Weblogic81Utils(String JavaDoc libDirPath, String JavaDoc domainDirPath) {
52         File JavaDoc libDir = new File JavaDoc(libDirPath);
53         if(!libDir.exists() || !libDir.canRead() || !libDir.isDirectory()) throw new IllegalArgumentException JavaDoc("Bad weblogic lib dir");
54         File JavaDoc weblogicJar = new File JavaDoc(libDir, "weblogic.jar");
55         File JavaDoc securityJar = new File JavaDoc(libDir, "jsafeFIPS.jar");
56         if(!weblogicJar.canRead()) throw new IllegalArgumentException JavaDoc("Cannot find JARs in provided lib dir");
57         domainDir = new File JavaDoc(domainDirPath);
58         if(!domainDir.exists() || !domainDir.canRead() || !domainDir.isDirectory()) throw new IllegalArgumentException JavaDoc("Bad domain directory");
59         File JavaDoc state = new File JavaDoc(domainDir, "SerializedSystemIni.dat");
60         if(!state.canRead()) throw new IllegalArgumentException JavaDoc("Cannot find serialized state in domain directory");
61         try {
62             ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(securityJar.exists() ? new URL JavaDoc[]{weblogicJar.toURL(), securityJar.toURL()} : new URL JavaDoc[]{weblogicJar.toURL()}, Weblogic81Utils.class.getClassLoader());
63             initialize(loader, state);
64         } catch (Exception JavaDoc e) {
65             throw (RuntimeException JavaDoc)new IllegalArgumentException JavaDoc("Unable to initialize encryption routines from provided arguments").initCause(e);
66         }
67     }
68
69     public Properties JavaDoc getBootProperties() {
70         File JavaDoc boot = new File JavaDoc(domainDir, "boot.properties");
71         FileInputStream JavaDoc bootIn = null;
72         try {
73             bootIn = new FileInputStream JavaDoc(boot);
74         } catch (FileNotFoundException JavaDoc e) {
75             return null;
76         }
77         try {
78             Properties JavaDoc props = new Properties JavaDoc();
79             props.load(bootIn);
80             bootIn.close();
81             for (Iterator JavaDoc it = props.keySet().iterator(); it.hasNext();) {
82                 String JavaDoc key = (String JavaDoc) it.next();
83                 String JavaDoc value = props.getProperty(key);
84                 if(value != null && value.startsWith("{")) props.setProperty(key, decryptString(value));
85             }
86             return props;
87         } catch (Exception JavaDoc e) {
88             return null;
89         }
90     }
91
92     public String JavaDoc getConfigXML() throws FileNotFoundException JavaDoc {
93         File JavaDoc config = new File JavaDoc(domainDir, "config.xml");
94         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(config));
95         StringWriter JavaDoc string = new StringWriter JavaDoc();
96         PrintWriter JavaDoc out = new PrintWriter JavaDoc(string);
97         String JavaDoc line;
98         Matcher JavaDoc m = ENCRYPTED_STRING.matcher("");
99         try {
100             while((line = in.readLine()) != null) {
101                 m.reset(line);
102                 int last = -1;
103                 while(m.find()) {
104                     out.print(line.substring(last+1, m.start()));
105                     String JavaDoc s = line.substring(m.start(), m.end());
106                     out.print("\"");
107                     out.print(decryptString(s.substring(1, s.length()-1)));
108                     out.print("\"");
109                     last = m.end()-1;
110                 }
111                 if(last == -1) {
112                     out.println(line);
113                 } else {
114                     if(line.length() > last+1) {
115                         out.print(line.substring(last+1));
116                     }
117                     out.println();
118                 }
119                 out.flush();
120             }
121             in.close();
122             out.close();
123         } catch (Exception JavaDoc e) {
124             return null;
125         }
126         return string.getBuffer().toString();
127     }
128
129     private void initialize(ClassLoader JavaDoc loader, File JavaDoc state) throws IOException JavaDoc, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc, ClassNotFoundException JavaDoc, InstantiationException JavaDoc {
130         byte[] salt = null, key = null;
131         FileInputStream JavaDoc in = new FileInputStream JavaDoc(state);
132         salt = readBytes(in);
133         int i = in.read();
134         if(i != -1) {
135             if(i != 1) throw new IllegalStateException JavaDoc();
136             key = readBytes(in);
137         }
138         in.close();
139         decrypter = getEncryptionService(loader, salt, key);
140         decoder = loader.loadClass("weblogic.utils.encoders.BASE64Decoder").newInstance();
141         decode = decoder.getClass().getMethod("decodeBuffer", new Class JavaDoc[]{String JavaDoc.class});
142         decrypt = decrypter.getClass().getMethod("decryptString", new Class JavaDoc[]{byte[].class});
143     }
144
145     private static byte[] readBytes(InputStream JavaDoc in) throws IOException JavaDoc {
146         int len = in.read();
147         if(len < 0)
148             throw new IOException JavaDoc("stream is empty");
149         byte result[] = new byte[len];
150         int index = 0;
151         while(true) {
152             if(index >= len) {
153                 break;
154             }
155             int count = in.read(result, index, len - index);
156             if(count == -1)
157                 break;
158             index += count;
159         }
160         return result;
161     }
162
163     private String JavaDoc decryptString(String JavaDoc string) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
164         if(string.indexOf('}') > -1) {
165             string = string.substring(string.indexOf("}")+1);
166         }
167         return (String JavaDoc) decrypt.invoke(decrypter, new Object JavaDoc[]{decode.invoke(decoder, new Object JavaDoc[]{string})});
168     }
169
170     static Object JavaDoc getEncryptionService(ClassLoader JavaDoc loader, byte salt[], byte key[]) throws NoSuchMethodException JavaDoc, ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
171         String JavaDoc magic = "0xccb97558940b82637c8bec3c770f86fa3a391a56";
172         Object JavaDoc factory = loader.loadClass("weblogic.security.internal.encryption.JSafeEncryptionServiceImpl").getMethod("getFactory", new Class JavaDoc[0]).invoke(null, null);
173         Method JavaDoc getter = factory.getClass().getMethod("getEncryptionService", new Class JavaDoc[]{byte[].class, String JavaDoc.class, byte[].class});
174         return getter.invoke(factory, new Object JavaDoc[]{salt, magic, key});
175     }
176 }
177
Popular Tags