KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > ConnectorHandlers


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  * CommonHandlers.java
26  *
27  */

28
29 package com.sun.enterprise.tools.admingui.handlers;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.EventObject JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.Enumeration JavaDoc;
36
37 import com.iplanet.jato.RequestContext;
38 import com.iplanet.jato.RequestContextImpl;
39 import com.iplanet.jato.RequestManager;
40 import com.iplanet.jato.model.DefaultModel;
41 import com.iplanet.jato.model.Model;
42 import com.iplanet.jato.util.RootCauseException;
43 import com.iplanet.jato.view.ContainerView;
44 import com.iplanet.jato.view.ContainerViewBase;
45 import com.iplanet.jato.view.View;
46 import com.iplanet.jato.view.DisplayFieldImpl;
47 import com.iplanet.jato.view.ViewBase;
48 import com.iplanet.jato.view.ViewBean;
49 import com.iplanet.jato.view.html.SelectableGroup;
50 import com.iplanet.jato.view.html.OptionList;
51
52 import javax.management.MBeanException JavaDoc;
53 import javax.management.ObjectName JavaDoc;
54 import javax.management.AttributeList JavaDoc;
55 import javax.management.Attribute JavaDoc;
56 import javax.servlet.ServletRequest JavaDoc;
57
58 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
59 import com.sun.enterprise.tools.guiframework.model.ModelManager;
60 import com.sun.enterprise.tools.guiframework.view.DescriptorCCPageTitle;
61 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
62 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
63 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
64 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor;
65 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent;
66 import com.sun.enterprise.tools.guiframework.view.event.ErrorEvent;
67
68 import com.sun.web.ui.model.CCActionTableModelInterface;
69 import com.sun.web.ui.model.CCPropertySheetModelInterface;
70 import com.sun.web.ui.model.CCActionTableModel;
71
72 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
73 import com.sun.enterprise.tools.admingui.util.Util;
74 import com.sun.enterprise.tools.admingui.tree.IndexTreeNode;
75 import com.sun.enterprise.tools.admingui.tree.IndexTreeModelImpl;
76 import com.sun.enterprise.tools.admingui.tree.IndexTreeModel;
77
78
79 /**
80  *
81  */

82 public class ConnectorHandlers {
83
84     public static final String JavaDoc PROP_MODEL = "property.model";
85     public static final String JavaDoc PROP_CHILD = "property.child";
86     public static final String JavaDoc PROPERTY_NAME = "propertyName";
87     public static final String JavaDoc PROPERTY_VALUE = "propertyValue";
88
89
90     /**
91      *
92      */

93     public void populateDropdown(RequestContext ctx, HandlerContext handlerCtx) {
94     View view = handlerCtx.getView();
95         // the child should be something like: com.sun.web.ui.view.html.CCDropDownMenu
96
SelectableGroup dropDownChild = (SelectableGroup) view;
97         // get the parent container of the dropdown
98
DescriptorContainerView parent = (DescriptorContainerView) dropDownChild.getParent();
99
100         // need the child descriptor to get a parameter, retrieve from the parent descriptor.
101
ViewDescriptor vd = parent.getViewDescriptor();
102         ViewDescriptor cvd = vd.getChildDescriptor(dropDownChild.getName());
103         // the method name is specified in the XML file.
104

105     String JavaDoc methodName = (String JavaDoc)ctx.getRequest().getAttribute("dropDownMethodName");
106     String JavaDoc objectName = (String JavaDoc)ctx.getRequest().getAttribute("dropDownObjectName");
107     String JavaDoc attr = (String JavaDoc)ctx.getRequest().getAttribute("dropDownAttributeName");
108     if (attr == null) {
109         throw new FrameworkException("attribute is null", cvd, view);
110     }
111     ctx.getRequest().setAttribute("rarName", attr);
112         Object JavaDoc[] params = new Object JavaDoc[]{attr};
113
114     if (objectName == null || methodName == null) {
115         throw new FrameworkException("objectName or methodName is null",
116         cvd, view);
117     }
118
119         // and finally fill in the menu choices.
120
dropDownChild.setLabelForNoneSelected(" ");
121         String JavaDoc[] type = new String JavaDoc[]{"java.lang.String"};
122     try {
123         Object JavaDoc keys = (Object JavaDoc) MBeanUtil.invoke(objectName, methodName, params, type);
124             String JavaDoc[] choices = (String JavaDoc[])keys;
125         //String [] choices = {"a","b","c"};
126

127         OptionList dropDownMenuOptions = new OptionList(choices,choices); // label and values
128
dropDownChild.setOptions(dropDownMenuOptions);
129     } catch (Exception JavaDoc ex) {
130         throw new FrameworkException(ex, cvd, view);
131     }
132     }
133
134
135     /**
136      *
137      */

138     public void populatePropsTableModel(RequestContext ctx, HandlerContext handlerCtx) {
139     View view = handlerCtx.getView();
140     if (!(handlerCtx.getEvent() instanceof BeforeCreateEvent)) {
141         ViewDescriptor desc = (view instanceof DescriptorContainerView) ?
142         ((DescriptorContainerView)view).getViewDescriptor() :
143         (ViewDescriptor)null;
144         throw new FrameworkException("This handler is for 'beforeCreate'" +
145         " handlers only!", desc, view);
146     }
147     ViewDescriptor desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor();
148     try {
149             if (desc instanceof CCActionTableDescriptor) {
150         CCActionTableDescriptor ccDesc = (CCActionTableDescriptor)desc;
151                 loadProperties(ccDesc.getModel(), ctx);
152         }
153         } catch (Exception JavaDoc ex) {
154             throw new FrameworkException(ex, desc, view);
155         }
156     }
157
158     /**
159      *
160      */

161     public void populateAdminPropsTableModel(RequestContext ctx, HandlerContext handlerCtx) {
162     View view = handlerCtx.getView();
163     if (!(handlerCtx.getEvent() instanceof BeforeCreateEvent)) {
164         ViewDescriptor desc = (view instanceof DescriptorContainerView) ?
165         ((DescriptorContainerView)view).getViewDescriptor() :
166         (ViewDescriptor)null;
167         throw new FrameworkException("This handler is for 'beforeCreate'" +
168         " handlers only!", desc, view);
169     }
170     ViewDescriptor desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor();
171     try {
172             if (desc instanceof CCActionTableDescriptor) {
173         CCActionTableDescriptor ccDesc = (CCActionTableDescriptor)desc;
174                 adminLoadProperties(ccDesc.getModel(), ctx);
175         }
176         } catch (Exception JavaDoc ex) {
177             throw new FrameworkException(ex, desc, view);
178         }
179     }
180
181
182
183     private void loadProperties(CCActionTableModelInterface model, RequestContext ctx) {
184     ServletRequest JavaDoc req = ctx.getRequest();
185     String JavaDoc methodName = (String JavaDoc)req.getAttribute("dropDownMethodName");
186     String JavaDoc objectName = (String JavaDoc)req.getAttribute("dropDownObjectName");
187     String JavaDoc attr = (String JavaDoc)req.getAttribute("rarName");
188     String JavaDoc cd = (String JavaDoc)req.getAttribute("connectionDefinition");
189         //String cd = "javax.resource.cci.ConnectionFactory";
190
//String attr = "comet";
191
if (attr == null) {
192         throw new FrameworkException("attribute is null");
193     }
194     if (cd == null) {
195         throw new FrameworkException("connection definition is null");
196     }
197
198         AttributeList JavaDoc attrList = new AttributeList JavaDoc();
199         attrList.add(new Attribute JavaDoc("resource-adapter-name", attr));
200         attrList.add(new Attribute JavaDoc("connection-definition-name", cd));
201         Object JavaDoc[] params = new Object JavaDoc[]{attrList};
202
203     if (objectName == null || methodName == null) {
204         throw new FrameworkException("objectName or methodName is null", null, null);
205     }
206         String JavaDoc[] type = new String JavaDoc[]{"javax.management.AttributeList"};
207
208     Object JavaDoc keys = (Object JavaDoc)MBeanUtil.invoke(objectName, methodName, params, type);
209         Properties JavaDoc props = (Properties JavaDoc)keys;
210
211         if (props == null) {
212             return; //nothing to load..
213
}
214
215     try {
216             Enumeration JavaDoc enums = props.propertyNames();
217             while(enums.hasMoreElements()){
218                 model.appendRow();
219                 String JavaDoc name = (String JavaDoc)enums.nextElement();
220                 String JavaDoc value = props.getProperty(name);
221                 model.setValue(PROPERTY_NAME, name);
222                 model.setValue(PROPERTY_VALUE, value);
223             }
224         } catch (Exception JavaDoc ex) {
225         throw new FrameworkException(ex);
226     }
227      }
228
229
230     private void adminLoadProperties(CCActionTableModelInterface model, RequestContext ctx) {
231     ServletRequest JavaDoc req = ctx.getRequest();
232     String JavaDoc methodName = (String JavaDoc)req.getAttribute("dropDownMethodName");
233     String JavaDoc objectName = (String JavaDoc)req.getAttribute("dropDownObjectName");
234     String JavaDoc attr = (String JavaDoc)req.getAttribute("JndiName");
235     String JavaDoc ra = (String JavaDoc)req.getAttribute("connectorModule");
236     if (attr == null) {
237         throw new FrameworkException("attribute is null");
238     }
239     if (ra == null) {
240         throw new FrameworkException("connector Module is null");
241     }
242
243         AttributeList JavaDoc attrList = new AttributeList JavaDoc();
244         attrList.add(new Attribute JavaDoc("resource-adapter-name", ra));
245         attrList.add(new Attribute JavaDoc("admin_object_interface", attr));
246         Object JavaDoc[] params = new Object JavaDoc[]{attrList};
247
248     if (objectName == null || methodName == null) {
249         throw new FrameworkException("objectName or methodName is null", null, null);
250     }
251         String JavaDoc[] type = new String JavaDoc[]{"javax.management.AttributeList"};
252
253     Object JavaDoc keys = (Object JavaDoc) MBeanUtil.invoke(objectName, methodName, params, type);
254         Properties JavaDoc props = (Properties JavaDoc)keys;
255
256         if (props == null) {
257             return; //nothing to load..
258
}
259
260     try {
261             Enumeration JavaDoc enums = props.propertyNames();
262             while(enums.hasMoreElements()){
263                 model.appendRow();
264                 String JavaDoc name = (String JavaDoc)enums.nextElement();
265                 String JavaDoc value = props.getProperty(name);
266                 model.setValue(PROPERTY_NAME, name);
267                 model.setValue(PROPERTY_VALUE, value);
268             }
269         } catch (Exception JavaDoc ex) {
270         throw new FrameworkException(ex);
271     }
272      }
273 }
274
Popular Tags