001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: RarConfigMBean.java 7585 2005-10-24 14:17:24Z pasmith $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.resource;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.TreeMap;
030:
031: import javax.management.MBeanException;
032:
033: import org.objectweb.jonas_lib.deployment.validation.JEntityResolver;
034: import org.objectweb.jonas_rar.deployment.api.ConnectorDTDs;
035: import org.objectweb.jonas_rar.deployment.api.ConnectorSchemas;
036: import org.objectweb.jonas_rar.deployment.api.JonasConnectorDTDs;
037: import org.objectweb.jonas_rar.deployment.api.JonasConnectorSchemas;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Node;
040: import org.w3c.dom.NodeList;
041: import org.xml.sax.EntityResolver;
042:
043: /**
044: * A RAR specific MBean which provides the schemas and EntityResolver
045: * needed to validate a jonas-ra.xml file or an ra.xml file.
046: * @author Patrick Smith
047: * @author Greg Lapouchnian
048: */
049: public class RarConfigMBean extends ArchiveConfigMBean {
050:
051: /**
052: * A default constructor which calls the parent constructor.
053: * @throws MBeanException from the super class.
054: */
055: public RarConfigMBean() throws MBeanException {
056: super ();
057: }
058:
059: /**
060: * Returns an EntityResolver for the schemas and dtds of
061: * ra.xml and jonas-ra.xml.
062: * @return an EntityResolver for the schemas and dtds
063: * for ra.xml and jonas-ra.xml.
064: */
065: public EntityResolver getEntityResolver() {
066: JEntityResolver jer = new JEntityResolver();
067:
068: // add both the generic and Jonas-specific schemas
069: jer.addSchemas(new ConnectorSchemas());
070: jer.addSchemas(new JonasConnectorSchemas());
071:
072: // add the connector DTDs
073: jer.addDtds(new ConnectorDTDs());
074: jer.addDtds(new JonasConnectorDTDs());
075:
076: return jer;
077: }
078:
079: /**
080: * Get a list of schemas available for RARs.
081: * @return a list of schemas for RAR validation.
082: */
083: protected List getSchemaList() {
084: // check if this schemas is part of connector schemas
085: List schemas = new ArrayList();
086: schemas.addAll(new ConnectorSchemas().getlocalSchemas());
087: schemas.addAll(new JonasConnectorSchemas().getlocalSchemas());
088: return schemas;
089: }
090:
091: public void updateXML(String archiveName, Map map) throws Exception {
092: String xmlFilePath = "META-INF/jonas-ra.xml";
093: Document doc = extractDocument(archiveName, xmlFilePath);
094:
095: Node poolParams = getPoolParams(doc);
096: TreeMap poolNames = getPoolTagMap();
097: if (poolParams != null) {
098: NodeList poolChildren = poolParams.getChildNodes();
099: for (int i = 0; i < poolChildren.getLength(); i++) {
100: String name = poolChildren.item(i).getNodeName();
101: if (poolNames.containsKey(name)) {
102: if (poolChildren.item(i).getFirstChild() != null
103: && poolChildren.item(i).getFirstChild()
104: .getNodeType() == Node.TEXT_NODE) {
105: poolChildren.item(i).getFirstChild()
106: .setNodeValue(
107: (String) map.get(poolNames
108: .get(name)));
109: } else {
110: poolChildren.item(i).appendChild(
111: doc.createTextNode((String) map
112: .get(poolNames.get(name))));
113: }
114: }
115: }
116: }
117:
118: Node jdbcParams = getJDBCParams(doc);
119: TreeMap jdbcNames = getJDBCTagMap();
120: if (jdbcParams != null) {
121: NodeList jdbcChildren = jdbcParams.getChildNodes();
122: for (int j = 0; j < jdbcChildren.getLength(); j++) {
123: String jdbcName = jdbcChildren.item(j).getNodeName();
124: if (jdbcNames.containsKey(jdbcName)) {
125: if (jdbcChildren.item(j).getFirstChild() != null
126: && jdbcChildren.item(j).getFirstChild()
127: .getNodeType() == Node.TEXT_NODE) {
128: jdbcChildren.item(j).getFirstChild()
129: .setNodeValue(
130: (String) map.get(jdbcNames
131: .get(jdbcName)));
132: } else {
133: jdbcChildren.item(j).appendChild(
134: doc.createTextNode((String) map
135: .get(jdbcNames.get(jdbcName))));
136: }
137: }
138: }
139: }
140:
141: verifyDocument(doc);
142: saveXML(archiveName, xmlFilePath, doc);
143: }
144:
145: protected TreeMap getJDBCTagMap() {
146: TreeMap names = new TreeMap();
147: names.put("jdbc-check-level", "jdbcConnCheckLevel");
148: names.put("jdbc-test-statement", "jdbcTestStatement");
149: return names;
150: }
151:
152: protected TreeMap getPoolTagMap() {
153: TreeMap names = new TreeMap();
154: names.put("pool-max-age", "connMaxAge");
155: names.put("pool-max-opentime", "maxOpentime");
156: names.put("pool-max", "maxSize");
157: names.put("pool-min", "minSize");
158: names.put("pool-max-waittime", "maxWaitTime");
159: names.put("pool-max-waiters", "maxWaiters");
160: names.put("pool-sampling-period", "samplingPeriod");
161: return names;
162: }
163:
164: protected Node getPoolParams(Document doc) {
165: if (doc.getElementsByTagName("pool-params").getLength() == 0) {
166: return null;
167: } else {
168: return doc.getElementsByTagName("pool-params").item(0);
169: }
170: }
171:
172: protected Node getJDBCParams(Document doc) {
173: if (doc.getElementsByTagName("jdbc-conn-params").getLength() == 0) {
174: return null;
175: } else {
176: return doc.getElementsByTagName("jdbc-conn-params").item(0);
177: }
178: }
179: }
|