KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > oc4j > ide > OC4JErrorManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.oc4j.ide;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.logging.ErrorManager JavaDoc;
27 import javax.swing.JOptionPane JavaDoc;
28 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
29 import org.netbeans.modules.j2ee.oc4j.OC4JDeploymentManager;
30 import org.netbeans.modules.j2ee.oc4j.util.OC4JDebug;
31 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils;
32 import org.openide.util.NbBundle;
33
34 /**
35  * Error manager for handling exceptions from Oracle Application Server
36  *
37  * @author Michal Mocnak
38  */

39 public class OC4JErrorManager extends ErrorManager JavaDoc {
40     
41     /**
42      * Singleton model pattern
43      */

44     private static Map JavaDoc<OC4JDeploymentManager, OC4JErrorManager> instances = new HashMap JavaDoc<OC4JDeploymentManager, OC4JErrorManager>();
45     
46     private OC4JDeploymentManager dm;
47     
48     /** Creates a new instance of OC4JErrorManager */
49     private OC4JErrorManager(OC4JDeploymentManager dm) {
50         this.dm = dm;
51     }
52     
53     /**
54      * Returns dm specific instance of OC4JErrorManager
55      *
56      * @param dm specified OC4JDeploymentManager
57      * @return instance of an OC4JErrorManager
58      */

59     public static OC4JErrorManager getInstance(OC4JDeploymentManager dm) {
60         if (!instances.containsKey(dm))
61             instances.put(dm, new OC4JErrorManager(dm));
62         
63         return instances.get(dm);
64     }
65     
66     private void write(String JavaDoc s) {
67         OC4JLogger.getInstance(dm.getUri()).write(s);
68     }
69     
70     private Throwable JavaDoc getDepestCause(Throwable JavaDoc t) {
71         return (null != t.getCause()) ? (getDepestCause(t.getCause())):(t);
72     }
73     
74     /**
75      * Do a reaction if any exists
76      *
77      * @param clazz class name what be searched for a reaction
78      * @return true if reaction is successfull or false if not
79      */

80     public synchronized boolean reaction(String JavaDoc clazz) {
81         ClassReaction c = ClassReaction.lookup(clazz);
82         
83         if (null != c) {
84             c.react(dm);
85             return true;
86         }
87         
88         return false;
89     }
90     
91     /**
92      * Parses exception's stack trace and try to performs reaction
93      *
94      * @param s debug message string
95      * @param e cause exception
96      * @param code error level
97      */

98     @Override JavaDoc
99     public synchronized void error(String JavaDoc s, Exception JavaDoc e, int code) {
100         List JavaDoc<String JavaDoc> buffer = new ArrayList JavaDoc<String JavaDoc>();
101         
102         if (OC4JDebug.isEnabled())
103             write(s);
104         
105         Throwable JavaDoc t = e;
106         
107         if (null != e.getCause()) {
108             t = getDepestCause(e.getCause());
109         }
110         
111         for (final StackTraceElement JavaDoc st : t.getStackTrace()) {
112             if (OC4JDebug.isEnabled())
113                 write(st.toString());
114             
115             // Do a reaction if available and if it wasn't done before
116
if(!buffer.contains(st.getClassName()))
117                 if (reaction(st.getClassName()))
118                     buffer.add(st.getClassName());
119         }
120     }
121     
122     // Class Reactions
123
static {
124         new AuthenticateReaction();
125         new MissingDriverReaction();
126         new MissingJSFReaction();
127     }
128     
129     private static class AuthenticateReaction extends ClassReaction {
130         
131         public AuthenticateReaction() {
132             super("oracle.oc4j.rmi.ClientRmiTransport");
133         }
134         
135         public void react(OC4JDeploymentManager manager) {
136             InstanceProperties ip = manager.getInstanceProperties();
137             
138             String JavaDoc username = ip.getProperty(InstanceProperties.USERNAME_ATTR);
139             String JavaDoc password = OC4JPluginUtils.requestPassword(username);
140             
141             if(password != null)
142                 ip.setProperty(InstanceProperties.PASSWORD_ATTR, password);
143         }
144     }
145     
146     private static class MissingDriverReaction extends ClassReaction {
147         
148         public MissingDriverReaction() {
149             super("java.net.URLClassLoader");
150         }
151         
152         public void react(OC4JDeploymentManager manager) {
153             JOptionPane.showMessageDialog(null, NbBundle.getMessage(OC4JErrorManager.class, "MSG_MissingDriver"),
154                     NbBundle.getMessage(OC4JErrorManager.class, "MSG_MissingDriverTitle"), JOptionPane.INFORMATION_MESSAGE);
155         }
156     }
157     
158     private static class MissingJSFReaction extends ClassReaction {
159         
160         public MissingJSFReaction() {
161             super("com.evermind.server.http.deployment.WARAnnotationParser");
162         }
163         
164         public void react(OC4JDeploymentManager manager) {
165             JOptionPane.showMessageDialog(null, NbBundle.getMessage(OC4JErrorManager.class, "MSG_MissingJSF"),
166                     NbBundle.getMessage(OC4JErrorManager.class, "MSG_MissingJSFTitle"), JOptionPane.INFORMATION_MESSAGE);
167         }
168     }
169     
170     private abstract static class ClassReaction {
171         
172         private static Map JavaDoc<String JavaDoc, ClassReaction> reactions = new HashMap JavaDoc<String JavaDoc, ClassReaction>();
173         
174         private String JavaDoc clazz;
175         
176         private ClassReaction(String JavaDoc clazz) {
177             this.clazz = clazz;
178             
179             reactions.put(clazz, this);
180         }
181         
182         private String JavaDoc getClazz() {
183             return clazz;
184         }
185         
186         public abstract void react(OC4JDeploymentManager manager);
187         
188         public static ClassReaction lookup(String JavaDoc clazz) {
189             return reactions.get(clazz);
190         }
191     }
192 }
193
Popular Tags