KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > descriptors > DataSheetDescriptor


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 package com.sun.enterprise.tools.admingui.descriptors;
25
26 import com.iplanet.jato.ModelManager;
27 import com.iplanet.jato.RequestManager;
28 import com.iplanet.jato.RequestContext;
29 import com.iplanet.jato.view.ContainerView;
30 import com.iplanet.jato.view.ContainerViewBase;
31 import com.iplanet.jato.view.View;
32 import com.sun.web.ui.model.CCManageChildrenModel;
33
34 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
35 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
36 import com.sun.enterprise.tools.guiframework.view.descriptors.FakeContainerDescriptor;
37 import com.sun.enterprise.tools.guiframework.util.LogUtil;
38
39 import com.sun.enterprise.tools.admingui.taglib.DataSheet;
40 import com.sun.enterprise.tools.admingui.taglib.DataSheetModelInterface;
41
42 import java.io.InputStream JavaDoc;
43 import java.net.URL JavaDoc;
44
45 import org.xml.sax.EntityResolver JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47
48
49 /**
50  *
51  */

52 public class DataSheetDescriptor extends ViewDescriptor implements FakeContainerDescriptor {
53
54     /**
55      * Constructor
56      */

57     public DataSheetDescriptor(String JavaDoc name) {
58     super(name);
59     }
60
61
62     public void registerChildren(ContainerViewBase instance) {
63     // Invoke the super registerChild
64
super.registerChildren(instance);
65
66     getModel().registerChildren(instance);
67     }
68     
69
70     /**
71      * This method creates empty descriptors on the fly for fields that are
72      * defined by the underlying model.
73      */

74     public ViewDescriptor getChildDescriptor(String JavaDoc name) {
75         // Do the normal stuff if we can...
76
ViewDescriptor desc = super.getChildDescriptor(name);
77     if (desc != null) {
78         return desc;
79     }
80
81     // Do we need to create a descriptor on the fly?
82
DataSheetModelInterface model = getModel();
83         if (model != null && model.isChildSupported(name)) {
84         // YES
85
desc = new DataSheetChildDescriptor(name);
86         // NOTE: This call is safe (mostly), because although we are
87
// NOTE: modifying a static structure... this change is applicable
88
// NOTE: to all users. However, there is a slight possibility of
89
// NOTE: of a sync. problem... not worth worrying about now. If
90
// NOTE: this does become a problem, just remove the
91
// NOTE: addChildDescriptor() line below.
92
addChildDescriptor(desc);
93         return desc;
94     }
95
96     return null;
97     }
98
99     class myEntityResolver implements EntityResolver JavaDoc {
100         public InputSource JavaDoc resolveEntity (String JavaDoc publicId, String JavaDoc systemId) {
101
102             if (systemId != null && (systemId.startsWith("http")==false) &&
103                 (systemId.endsWith(".xml") || systemId.endsWith(".dtd"))) {
104                 InputStream JavaDoc resourceStream = null;
105                 int i = systemId.indexOf("dtd/");
106                 if (i >= 0) {
107                     systemId = "xml/" + systemId.substring(i);
108                     //System.out.println("systemID: " + systemId);
109
// First look in the classpath, the file maybe in a JAR
110
resourceStream = getClass().getClassLoader()
111                         .getResourceAsStream(systemId);
112                     if (resourceStream == null) {
113                         // Next look in the app directory.
114
String JavaDoc sURL = "file:///" +
115                             RequestManager.getRequestContext().getServletContext()
116                             .getRealPath(systemId);
117                         //System.out.println("sURL: " + sURL);
118
try {
119                             resourceStream = new URL JavaDoc(sURL).openStream();
120                         } catch (Exception JavaDoc ex) {
121                             //System.out.println(":::::::::::"+ex.getMessage());
122
}
123                     }
124                 }
125                 if (resourceStream != null) {
126                     return new InputSource JavaDoc(resourceStream);
127                 }
128             }
129             // use the default behaviour
130
return null;
131         }
132     }
133     
134     /**
135      * <P>This method will get the CCPropertySheetModel for this property
136      * sheet. If one has not already been created, it will create a new one.
137      * This method depends on the "XML_FILE" property being defined. You must
138      * also have a model mapping entry in the "ModelTypeMap" for
139      * "DataSheetModelInterface.class".</P>
140      *
141      * <P>Optionally, you may specify whether this model should be retrieved
142      * and/or stored from/in session. The GET_FROM_SESSION and
143      * STORE_IN_SESSION parameters control this.</P>
144      */

145     public DataSheetModelInterface getModel() {
146     // Determine if the session should be used w/ the model manager.
147
boolean fromSession = shouldGetModelFromSession();
148     boolean toSession = shouldPutModelToSession();
149     String JavaDoc instanceName = getModelInstanceName();
150
151     // Use the ModelManager to create/get the model
152
ModelManager mgr = RequestManager.getRequestContext().getModelManager();
153     DataSheetModelInterface model =
154         (DataSheetModelInterface)mgr.getModel(
155                 DataSheetModelInterface.class,
156         instanceName, fromSession, toSession);
157
158     // Make sure the XML file is set on the Model
159
if (model.getDocument() == null) {
160         InputStream JavaDoc in = getXMLFileAsStream();
161             model.setEntityResolver(new DataSheetDescriptor.myEntityResolver());
162 // String resolverName = null;
163
// EntityResolver er = null;
164
// try {
165
// resolverName = (String)this.getParameter("EntityResolverClassName");
166
// if (resolverName != null && model instanceof CCManageChildrenModel) {
167
// er = (EntityResolver) Class.forName(resolverName).newInstance();
168
// model.setEntityResolver(er);
169
// }
170
// } catch (Exception ex) {
171
// if (LogUtil.isLoggable(LogUtil.WARNING)) {
172
// LogUtil.log(LogUtil.WARNING, resolverName+": "+ex.getMessage());
173
// }
174
// }
175
model.setDocument(in);
176         try {
177         in.close();
178         } catch (java.io.IOException JavaDoc ex) {
179         // Ignore
180
}
181         }
182     return model;
183     }
184
185
186     /**
187      * This is a factory method for CCActionTable instances.
188      *
189      * @param ctx The RequestContext
190      * @param container The container for the newly created
191      */

192     public View getInstance(RequestContext ctx, ContainerView container, String JavaDoc name) {
193     return new DescriptorDataSheet(ctx, container, name, this, getModel());
194     }
195 }
196
Popular Tags