001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/settings/OjbBootStrap.java $
003: * $Id: OjbBootStrap.java 11400 2006-06-29 20:24:36Z lydial@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.settings;
021:
022: import java.util.Properties;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.ojb.broker.PBKey;
027: import org.apache.ojb.broker.PersistenceBrokerFactory;
028: import org.apache.ojb.broker.metadata.ConnectionPoolDescriptor;
029: import org.apache.ojb.broker.metadata.ConnectionRepository;
030: import org.apache.ojb.broker.metadata.DescriptorRepository;
031: import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
032: import org.apache.ojb.broker.metadata.MetadataManager;
033: import org.apache.ojb.broker.metadata.SequenceDescriptor;
034:
035: /**
036: * Bootstaps the OJB configuration.
037: *
038: * @author <a href="mailto:bmcgough@indiana.edu">Brian McGough</a>
039: * @author <a href="mailto:lance@indiana.edu">Lance Speelmon</a>
040: * @version $Id: OjbBootStrap.java 11400 2006-06-29 20:24:36Z lydial@stanford.edu $
041: */
042: public class OjbBootStrap {
043: private static Log log = LogFactory.getLog(OjbBootStrap.class);
044:
045: /**
046: * Creates a new OjbBootStrap object.
047: */
048: public OjbBootStrap() {
049: if (log.isDebugEnabled()) {
050: log.debug("new OjbBootStrap()");
051: }
052: }
053:
054: /**
055: * DOCUMENTATION PENDING
056: *
057: * @throws IllegalStateException DOCUMENTATION PENDING
058: */
059: public void bootstrap() {
060: log.debug("bootstrap()");
061: try {
062: PathInfo path = PathInfo.getInstance();
063: if ((path.getBasePathToSecurity() == null)
064: || (path.getBasePathToSettings() == null)
065: || (path.getPathToSecurity() == null)
066: || (path.getPathToSettings() == null)) {
067: log.fatal("PathInfo not initialized properly");
068: throw new IllegalStateException(
069: "PathInfo not initialized properly");
070: }
071:
072: Properties securityProperties = path
073: .getSecurityProperties("SAM.properties");
074: Properties settingsProperties = path
075: .getSettingsProperties("SAM.properties");
076: if ((securityProperties == null)
077: || (settingsProperties == null)) {
078: log.fatal("Could not open Properties");
079: throw new IllegalStateException(
080: "Could not open Properties");
081: }
082:
083: if (!securityProperties.containsKey("username")
084: || !securityProperties.containsKey("password")) {
085: log
086: .fatal("Properties do not contain username or password");
087: throw new IllegalStateException(
088: "Properties do not contain username or password");
089: }
090:
091: if (!settingsProperties.containsKey("jcd-alias")
092: || !settingsProperties.containsKey("platform")
093: || !settingsProperties.containsKey("jdbc-level")
094: || !settingsProperties.containsKey("driver")
095: || !settingsProperties.containsKey("protocol")
096: || !settingsProperties.containsKey("subprotocol")
097: || !settingsProperties.containsKey("dbalias")
098: || !settingsProperties
099: .containsKey("repositoryXmlFilePath")
100: || !settingsProperties
101: .containsKey("maxActivePersistenceBrokers")
102: || !settingsProperties
103: .containsKey("sequenceManagerClass")
104: || !settingsProperties
105: .containsKey("validationQuery")
106: || !settingsProperties.containsKey("testOnBorrow")
107: || !settingsProperties.containsKey("testWhileIdle")) {
108: log.fatal("Missing one or more required keys");
109: throw new IllegalStateException(
110: "Missing one or more required keys");
111: }
112:
113: JdbcConnectionDescriptor jdbcConnectionDescriptor = new JdbcConnectionDescriptor();
114: jdbcConnectionDescriptor.setJcdAlias(settingsProperties
115: .getProperty("jcd-alias"));
116: jdbcConnectionDescriptor.setUserName(securityProperties
117: .getProperty("username"));
118: jdbcConnectionDescriptor.setPassWord(securityProperties
119: .getProperty("password"));
120: jdbcConnectionDescriptor.setDbAlias(settingsProperties
121: .getProperty("dbalias"));
122: jdbcConnectionDescriptor.setDbms(settingsProperties
123: .getProperty("platform"));
124: jdbcConnectionDescriptor.setDefaultConnection(true);
125: jdbcConnectionDescriptor.setJdbcLevel(settingsProperties
126: .getProperty("jdbc-level"));
127: jdbcConnectionDescriptor.setDriver(settingsProperties
128: .getProperty("driver"));
129: jdbcConnectionDescriptor.setProtocol(settingsProperties
130: .getProperty("protocol"));
131: jdbcConnectionDescriptor.setSubProtocol(settingsProperties
132: .getProperty("subprotocol"));
133:
134: SequenceDescriptor sequenceDescriptor = new SequenceDescriptor(
135: jdbcConnectionDescriptor);
136: sequenceDescriptor.setSequenceManagerClass(Class
137: .forName(settingsProperties
138: .getProperty("sequenceManagerClass")));
139: sequenceDescriptor
140: .setConfigurationProperties(new Properties());
141: jdbcConnectionDescriptor
142: .setSequenceDescriptor(sequenceDescriptor);
143:
144: ConnectionPoolDescriptor connectionPoolDescriptor = new ConnectionPoolDescriptor();
145: connectionPoolDescriptor
146: .setMaxActive(Integer
147: .parseInt(settingsProperties
148: .getProperty("maxActivePersistenceBrokers")));
149: connectionPoolDescriptor
150: .setValidationQuery(settingsProperties
151: .getProperty("validationQuery"));
152: connectionPoolDescriptor.setTestOnBorrow("true"
153: .equalsIgnoreCase(settingsProperties
154: .getProperty("testOnBorrow")));
155: connectionPoolDescriptor.setTestWhileIdle("true"
156: .equalsIgnoreCase(settingsProperties
157: .getProperty("testWhileIdle")));
158: jdbcConnectionDescriptor
159: .setConnectionPoolDescriptor(connectionPoolDescriptor);
160:
161: MetadataManager mm = MetadataManager.getInstance();
162: if (mm != null) {
163: ConnectionRepository connectionRepository = mm
164: .connectionRepository();
165: log.debug("connection repository object value: "
166: + connectionRepository);
167: connectionRepository
168: .addDescriptor(jdbcConnectionDescriptor);
169: } else {
170: throw new RuntimeException(
171: "Error is MetadataManager is null");
172: }
173:
174: // MetdataManager m = MetadataManager.getInstance();
175: //
176: // if (m != null){
177: // ConnectionRepository connectionRepository =
178: // m.connectionRepository();
179: // }
180: //
181: // connectionRepository.addDescriptor(jdbcConnectionDescriptor);
182:
183: DescriptorRepository descriptorRepostitory = MetadataManager
184: .getInstance()
185: .readDescriptorRepository(
186: settingsProperties
187: .getProperty("repositoryXmlFilePath"));
188: MetadataManager.getInstance().mergeDescriptorRepository(
189: descriptorRepostitory);
190:
191: //MetadataManager.getInstance().setDescriptor(descriptorRepostitory, true);
192: PersistenceBrokerFactory.setDefaultKey(new PBKey(
193: settingsProperties.getProperty("jcd-alias"),
194: securityProperties.getProperty("username"),
195: securityProperties.getProperty("password")));
196: log.info("started: "
197: + settingsProperties.getProperty("jcd-alias"));
198: } catch (Exception e) {
199: log.error(e.getMessage(), e);
200: throw new IllegalStateException(e.getMessage());
201: }
202: }
203: }
|