001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.jbi.jsf.bean;
037:
038: import com.sun.data.provider.TableDataProvider;
039: import com.sun.data.provider.impl.ObjectListDataProvider;
040:
041: import java.io.StringReader;
042: import java.util.ArrayList;
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.Set;
046: import java.util.Properties;
047: import java.util.logging.Logger;
048:
049: import javax.xml.parsers.DocumentBuilder;
050: import javax.xml.parsers.DocumentBuilderFactory;
051:
052: import com.sun.jbi.jsf.util.BeanUtilities;
053: import com.sun.jbi.jsf.util.JBILogger;
054: import com.sun.jbi.jsf.util.ValidationUtilities;
055: import com.sun.jbi.ui.common.JBIAdminCommands;
056:
057: import org.w3c.dom.Document;
058: import org.w3c.dom.NodeList;
059: import org.w3c.dom.Node;
060: import org.w3c.dom.Element;
061: import org.w3c.dom.Text;
062: import org.xml.sax.InputSource;
063:
064: import com.sun.org.apache.xpath.internal.XPathAPI;
065:
066: /**
067: * Provides properties used to configure SE or BC just before installation take
068: place
069: */
070: public class JBIComponentConfigBean {
071:
072: /** Creates a new instance of JBIComponentConfigBean */
073: public JBIComponentConfigBean() {
074: isDiffrentComponent = true;
075: }
076:
077: public TableDataProvider getComponentNewConfigurationData() {
078: List<ComponentConfigurationEntry> newConfigData = new ArrayList<ComponentConfigurationEntry>();
079: componentNewConfigurationDataProvider = new ObjectListDataProvider(
080: newConfigData);
081: if (!isValidComponent) {
082: return componentNewConfigurationDataProvider;
083: }
084:
085: logger
086: .fine("JBIComponentConfigBean.getNewComponentConfigurationeData(): result="
087: + componentNewConfigurationDataProvider);
088:
089: return componentNewConfigurationDataProvider;
090: }
091:
092: public TableDataProvider getComponentConfigurationData() {
093:
094: if (componentConfigurationDataProvider != null
095: && !isDiffrentComponent) {
096: componentConfigurationDataProvider.commitChanges();
097: } else {
098: logger
099: .fine("JBIComponentConfigBean.getComponentConfigurationeData()");
100:
101: ArchiveBean archiveBean = BeanUtilities.getArchiveBean();
102: // get the configuration properties from the component jbi.xml
103: List<ComponentConfigurationEntry> configProperties = null;
104: if (!isDomainDataSource) {
105: configProperties = parseConfigData(archiveBean);
106: } else {
107: configProperties = getComponentConfigurationDataFromDomain();
108: }
109: componentConfigurationDataProvider = new ObjectListDataProvider(
110: configProperties);
111:
112: logger
113: .fine("JBIComponentConfigBean.getComponentConfigurationeData(): configProperties="
114: + componentConfigurationDataProvider);
115:
116: isDiffrentComponent = false;
117:
118: }
119: return componentConfigurationDataProvider;
120: }
121:
122: private List<ComponentConfigurationEntry> parseJbiXMLData(
123: Document jbiXMLDoc) {
124: List<ComponentConfigurationEntry> configData = new ArrayList<ComponentConfigurationEntry>();
125: logger
126: .fine("JBIComponentConfigBean.parseJbiXMLData(): configProperties:");
127: try {
128: NodeList configurationNodes = XPathAPI.selectNodeList(
129: jbiXMLDoc, XPATH_CONFIGURATION);
130: // jbi.xml should have at most 1 configuration node
131: if (configurationNodes.getLength() > 0) {
132: {
133: NodeList configElementsList = configurationNodes
134: .item(0).getChildNodes();
135: int count = configElementsList.getLength();
136: for (int i = 0; i < count; ++i) {
137: Node node = configElementsList.item(i);
138: if ((node != null) && (node instanceof Element)) {
139: String name = node.getNodeName();
140: Text text = (Text) node.getChildNodes()
141: .item(0);
142: String value = null;
143: if (text != null) {
144: value = text.getData();
145: }
146: if (name != null) {
147: if (value == null) {
148: value = "";
149: }
150: // remove namespace from name
151: name = name.substring(name
152: .lastIndexOf(":") + 1);
153: logger.fine("name=" + name + " value="
154: + value);
155: ComponentConfigurationEntry entry = new ComponentConfigurationEntry(
156: name, value, value, false);
157: configData.add(entry);
158: }
159: }
160: }
161:
162: }
163: }
164: } catch (Exception ex) {
165: // TBD use logging warning
166: logger.fine("getType caught ex=" + ex);
167: ex.printStackTrace(System.err);
168: }
169: return configData;
170: }
171:
172: public Properties getConfigurationProperties() {
173: Properties configProps = new Properties();
174: if (componentConfigurationDataProvider != null
175: && componentConfigurationDataProvider.getRowCount() > 0) {
176: configProps = getPropertiesFromProvider(componentConfigurationDataProvider);
177: }
178: Properties additionalProperties = getPropertiesFromProvider(componentNewConfigurationDataProvider);
179: // merge jbi properties with the additional properties user
180: // added during install
181: configProps = mergeProperties(configProps, additionalProperties);
182: return configProps;
183: }
184:
185: public void invalidateCommittedDataProvider() {
186: componentConfigurationDataProvider = null;
187: }
188:
189: public String getDomainDataSource() {
190: // since this call is made at the beginning of manage target
191: // screen we assume we deal we diffrent component
192: isDiffrentComponent = true;
193: isDomainDataSource = true;
194: return "domain";
195: }
196:
197: public String getArchiveDataSource() {
198: // since this call is made at the beginning of step 2
199: // of new component installation we assume we deal with new component
200: isDiffrentComponent = true;
201: isDomainDataSource = false;
202: return "Archive";
203: }
204:
205: public String getBindorServiceComponent() {
206: isValidComponent = true;
207: return "component";
208: }
209:
210: public String getAssemblyComponent() {
211: isValidComponent = false;
212: return "service-assembly";
213: }
214:
215: public String getShareLibComponent() {
216: isValidComponent = false;
217: return "shared-false";
218: }
219:
220: public boolean isConfigurationValidForComponent() {
221: return isValidComponent;
222: }
223:
224: private List<ComponentConfigurationEntry> getComponentConfigurationDataFromDomain() {
225: List<ComponentConfigurationEntry> configData = new ArrayList<ComponentConfigurationEntry>();
226: if (!isValidComponent) {
227: return configData;
228: }
229: JBIAdminCommands jbiAdminCommands = BeanUtilities.getClient();
230: logger
231: .fine("JBIComponentConfigBean.getComponentConfigurationDataFromDomain(): JBIAdminCommands="
232: + jbiAdminCommands);
233:
234: ShowBean showBean = BeanUtilities.getShowBean();
235: String componentName = showBean.getName();
236: try {
237: String jbiXMLData = jbiAdminCommands
238: .getComponentInstallationDescriptor(componentName);
239: DocumentBuilderFactory factory = DocumentBuilderFactory
240: .newInstance();
241: DocumentBuilder db = factory.newDocumentBuilder();
242: InputSource inStream = new InputSource();
243: inStream.setCharacterStream(new StringReader(jbiXMLData));
244: Document jbiXMLDoc = db.parse(inStream);
245: logger
246: .fine("JBIComponentConfigBean.getComponentConfigurationDataFromDomain(): jbi.XML Document="
247: + jbiXMLDoc);
248: JBIComponentConfigBean configBean = new JBIComponentConfigBean();
249: configData = configBean.parseJbiXMLData(jbiXMLDoc);
250: } catch (Exception e) {
251: e.printStackTrace();
252: }
253: return configData;
254: }
255:
256: private Properties getPropertiesFromProvider(
257: ObjectListDataProvider dataProvider) {
258: logger
259: .fine("JBIComponentConfigBean.getPropertiesFromProvider():");
260: Properties configProps = new Properties();
261: dataProvider.commitChanges();
262: List<ComponentConfigurationEntry> origList = dataProvider
263: .getList();
264: for (ComponentConfigurationEntry entry : origList) {
265: String name = entry.getName();
266: String value = entry.getDefaultValue();
267: String newValue = entry.getNewValue();
268: if (newValue != null && !newValue.equals(value)) {
269: value = newValue;
270: }
271: configProps.setProperty(name, value);
272: logger.fine("name=" + name + " value=" + value);
273: }
274:
275: return configProps;
276: }
277:
278: private Properties mergeProperties(Properties configProps,
279: Properties newProperties) {
280: Properties jbiXMLProps = configProps;
281: // add and update(on key match) properties
282: Set keys = newProperties.keySet();
283: for (Iterator iter = keys.iterator(); iter.hasNext();) {
284: String key = (String) iter.next();
285: String value = (String) newProperties.get(key);
286: jbiXMLProps.setProperty(key, value);
287: }
288: return jbiXMLProps;
289: }
290:
291: private List<ComponentConfigurationEntry> parseConfigData(
292: ArchiveBean archiveBean) {
293: List<ComponentConfigurationEntry> configData = new ArrayList<ComponentConfigurationEntry>();
294: if (!archiveBean.getHasJbiXml() || !isValidComponent) {
295: return configData;
296: }
297: Document jbiXMLDoc = ValidationUtilities.getJbiDocument();
298: return parseJbiXMLData(jbiXMLDoc);
299: }
300:
301: /**
302: * Controls printing of diagnostic messages to the log
303: */
304: //Get Logger to log fine mesages for debugging
305: private static Logger logger = JBILogger.getInstance();
306:
307: private final static String XPATH_CONFIGURATION = "/jbi/component/Configuration";
308:
309: private ObjectListDataProvider componentConfigurationDataProvider;
310: private ObjectListDataProvider componentNewConfigurationDataProvider;
311: private boolean isDomainDataSource;
312: private boolean isDiffrentComponent;
313: // workaround for jsftemplating conditional statement and tables problem.
314: private boolean isValidComponent;
315: }
|