001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.appservers.security.jboss;
018:
019: import javax.naming.Context;
020: import javax.naming.InitialContext;
021: import javax.naming.NamingException;
022: import javax.sql.DataSource;
023:
024: import org.apache.jetspeed.security.UserManager;
025: import org.apache.ojb.broker.PBKey;
026: import org.apache.ojb.broker.accesslayer.ConnectionFactoryManagedImpl;
027: import org.apache.ojb.broker.metadata.ConnectionPoolDescriptor;
028: import org.apache.ojb.broker.metadata.ConnectionRepository;
029: import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
030: import org.apache.ojb.broker.metadata.JdbcMetadataUtils;
031: import org.apache.ojb.broker.metadata.MetadataManager;
032: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
033: import org.springframework.context.support.GenericApplicationContext;
034: import org.springframework.core.io.ClassPathResource;
035:
036: public class JetspeedSecurityService implements
037: JetspeedSecurityServiceMBean {
038:
039: private final String JCD_ALIAS = "JetspeedSecurityServiceDS";
040:
041: private GenericApplicationContext ctx;
042:
043: /**
044: * Create a new security service. The service's implementation is based on a <a
045: * href="www.springframework.org">Spring</a> application that is assembled from the configuration files in
046: * <code>META-INF/jboss-secsvc</code>.
047: */
048: public JetspeedSecurityService() {
049: // Prepare JCD so that it can be resolved
050: JdbcConnectionDescriptor jcd = findJcd();
051: if (jcd == null) {
052: // JCD not found, initialize
053: jcd = new JdbcConnectionDescriptor();
054: jcd.setJcdAlias(JCD_ALIAS);
055: ConnectionPoolDescriptor cpd = new ConnectionPoolDescriptor();
056: cpd
057: .setConnectionFactory(ConnectionFactoryManagedImpl.class);
058: jcd.setConnectionPoolDescriptor(cpd);
059: ConnectionRepository cr = MetadataManager.getInstance()
060: .connectionRepository();
061: cr.addDescriptor(jcd);
062: }
063: // Instatiating application
064: ctx = new GenericApplicationContext();
065: XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
066: ctx);
067: xmlReader.loadBeanDefinitions(new ClassPathResource(
068: "META-INF/jboss-secsvc/jboss-security-service.xml"));
069: ctx.refresh();
070: }
071:
072: private JdbcConnectionDescriptor findJcd() {
073: // Try to find JCD
074: ConnectionRepository cr = MetadataManager.getInstance()
075: .connectionRepository();
076: return cr.getDescriptor(new PBKey(JCD_ALIAS));
077: }
078:
079: /**
080: * Set the JNDI name of the <code>DataSource</code> to be used to access the database.
081: *
082: * @param jndiName
083: */
084: public void setDataSourceJndiName(String jndiName) {
085: JdbcConnectionDescriptor jcd = findJcd();
086: try {
087: Context initialContext = new InitialContext();
088: DataSource ds = (DataSource) initialContext
089: .lookup(jndiName);
090: (new JdbcMetadataUtils()).fillJCDFromDataSource(jcd, ds,
091: null, null);
092: } catch (NamingException e) {
093: throw (IllegalArgumentException) (new IllegalArgumentException(
094: "Data source \"" + jndiName
095: + "\" not found in JNDI: " + e.getMessage()))
096: .initCause(e);
097: }
098: jcd.setDatasourceName(jndiName);
099: }
100:
101: /**
102: * Get the JNDI name of the <code>DataSource</code> used to access the database.
103: *
104: * @return jndiName
105: */
106: public String getDataSourceJndiName() {
107: JdbcConnectionDescriptor jcd = findJcd();
108: if (jcd == null) {
109: return null;
110: }
111: return jcd.getDatasourceName();
112: }
113:
114: /**
115: * @see JetspeedSecurityServiceMBean#getUserManager()
116: */
117: public UserManager getUserManager() {
118: UserManager um = (UserManager) ctx
119: .getBean("org.apache.jetspeed.security.UserManager");
120: return um;
121: }
122: }
|