KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > server > util > PreprocessorUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * @(#) PreprocessorUtil.java
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  *
31  * This software is the confidential and proprietary information
32  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
33  * You shall not disclose such Confidential Information and shall
34  * use it only in accordance with the terms of the license
35  * agreement you entered into with iPlanet/Sun Microsystems.
36  */

37
38 package com.sun.appserv.server.util;
39
40 import java.util.Hashtable JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43 import com.sun.appserv.BytecodePreprocessor;
44 import com.sun.common.util.logging.LogDomains;
45
46 /**
47  * PreprocessorUtil is a utility class for managing the bytecode
48  * preprocessor(s). The list of preprocessors are passed in as a string array
49  * to the initialize method. If there is a problem initialize any of the
50  * preprocessors, all preprocessing is disabled.
51  */

52 public class PreprocessorUtil {
53     
54     private static boolean _preprocessorEnabled = false;
55     private static BytecodePreprocessor[] _preprocessor;
56     
57     /**
58      * Initializes the preprocessor utility with the associated class names
59      * array arugment.
60      * @param ppClassNames - the String array of preprocessor class names.
61      * @return - true if successful, otherwise false. All preprocessors must
62      * successfully initialize for true to be returned.
63      */

64     public static boolean init (String JavaDoc[] ppClassNames) {
65         if (ppClassNames != null) {
66             setupPreprocessor(ppClassNames);
67         }
68         return _preprocessorEnabled;
69     }
70
71     /**
72      * Processes a class through the preprocessor.
73      * @param className - the class name.
74      * @param classBytes - the class byte array.
75      * @return - the processed class byte array.
76      */

77     public static byte[] processClass (String JavaDoc className, byte[] classBytes) {
78         Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.CMN_LOGGER);
79         byte[] goodBytes = classBytes;
80         if (_preprocessorEnabled) {
81             if (_preprocessor != null) {
82                 // Loop through all of the defined preprocessors...
83
for (int i=0; i < _preprocessor.length; i++) {
84                     classBytes =
85                         _preprocessor[i].preprocess(className, classBytes);
86                     _logger.fine("[PreprocessorUtil.processClass] Preprocessor "
87                         + i + " Processed Class: " + className);
88                     // Verify the preprocessor returned some bytes
89
if (classBytes != null){
90                         goodBytes = classBytes;
91                     }
92                     else{
93                         _logger.log(Level.SEVERE,
94                             "bytecodepreprocessor.preprocess_failed",
95                             new String JavaDoc[] {className,
96                                           _preprocessor[i].getClass().getName()});
97                             
98                         // If were on the 1st preprocessor
99
if (i == 0){
100                             _logger.log(
101                                 Level.SEVERE,
102                                 "bytecodepreprocessor.resetting_original",
103                                 className);
104                         }
105                         // We're on the 2nd or nth preprocessor.
106
else{
107                             _logger.log(
108                                 Level.SEVERE,
109                                 "bytecodepreprocessor.resetting_last_good",
110                                 className);
111                         }
112                     }
113                 }
114             }
115         }
116         return goodBytes;
117     }
118     
119     private synchronized static void setupPreprocessor(String JavaDoc[] ppClassNames) {
120         Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.CMN_LOGGER);
121
122         if (_preprocessor != null)
123             // The preprocessors have already been set up.
124
return;
125
126         try {
127             _preprocessor = new BytecodePreprocessor[ppClassNames.length];
128             for (int i = 0; i < ppClassNames.length; i++) {
129                 String JavaDoc ppClassName = ppClassNames[i].trim();
130                 Class JavaDoc ppClass = Class.forName(ppClassName);
131                 if (ppClass != null){
132                     _preprocessor[i] = (BytecodePreprocessor)
133                                                         ppClass.newInstance();
134                     if (_preprocessor[i] instanceof BytecodePreprocessor){
135                         _preprocessor[i] =
136                             (BytecodePreprocessor)_preprocessor[i];
137                         _preprocessorEnabled = true;
138                     } else {
139                         _logger.log(Level.SEVERE,
140                             "bytecodepreprocessor.invalid_type",
141                             ppClassName);
142                         _logger.log(Level.SEVERE,
143                             "bytecodepreprocessor.disabled");
144                         _preprocessorEnabled = false;
145                     }
146                 }
147                 if (_preprocessor[i] != null){
148                     if (!_preprocessor[i].initialize(new Hashtable JavaDoc())) {
149                         _logger.log(Level.SEVERE,
150                             "bytecodepreprocessor.failed_init",
151                             ppClassName);
152                         _logger.log(Level.SEVERE,
153                             "bytecodepreprocessor.disabled");
154                         _preprocessorEnabled = false;
155                     }
156                 } else {
157                     _logger.log(Level.SEVERE,
158                         "bytecodepreprocessor.failed_init",
159                         ppClassName);
160                     _logger.log(Level.SEVERE,
161                         "bytecodepreprocessor.disabled");
162                     _preprocessorEnabled = false;
163                 }
164             }
165         } catch (Throwable JavaDoc t) {
166             _logger.log(Level.SEVERE, "bytecodepreprocessor.setup_ex", t);
167             _logger.log(Level.SEVERE, "bytecodepreprocessor.disabled");
168             _preprocessorEnabled = false;
169             return;
170         }
171     }
172
173     /**
174      * Indicates whether or not the preprocessor is enabled
175      * @return - true of the preprocessor is enabled, otherwise false.
176      */

177     public static boolean isPreprocessorEnabled() {
178         return _preprocessorEnabled;
179     }
180 }
181
Popular Tags