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: */package org.apache.openejb.core.ivm.naming;
017:
018: import java.util.Hashtable;
019: import java.util.Properties;
020:
021: import javax.naming.Context;
022: import javax.naming.AuthenticationException;
023: import javax.security.auth.login.LoginException;
024:
025: import org.apache.openejb.EnvProps;
026: import org.apache.openejb.loader.SystemInstance;
027: import org.apache.openejb.spi.ContainerSystem;
028: import org.apache.openejb.spi.SecurityService;
029:
030: public class InitContextFactory implements
031: javax.naming.spi.InitialContextFactory {
032:
033: public Context getInitialContext(Hashtable env)
034: throws javax.naming.NamingException {
035: if (!org.apache.openejb.OpenEJB.isInitialized()) {
036: initializeOpenEJB(env);
037: }
038:
039: String user = (String) env.get(Context.SECURITY_PRINCIPAL);
040: String pass = (String) env.get(Context.SECURITY_CREDENTIALS);
041: String realmName = (String) env
042: .get("openejb.authentication.realmName");
043:
044: if (user != null && pass != null) {
045: try {
046: SecurityService securityService = SystemInstance.get()
047: .getComponent(SecurityService.class);
048: Object identity = null;
049: if (realmName == null) {
050: identity = securityService.login(user, pass);
051: } else {
052: identity = securityService.login(realmName, user,
053: pass);
054: }
055: securityService.associate(identity);
056: } catch (LoginException e) {
057: throw (AuthenticationException) new AuthenticationException(
058: "User could not be authenticated: " + user)
059: .initCause(e);
060: }
061: }
062:
063: ContainerSystem containerSystem = SystemInstance.get()
064: .getComponent(ContainerSystem.class);
065: Context context = containerSystem.getJNDIContext();
066: context = (Context) context.lookup("java:openejb/ejb");
067: return context;
068:
069: }
070:
071: private void initializeOpenEJB(Hashtable env)
072: throws javax.naming.NamingException {
073: try {
074: Properties props = new Properties();
075:
076: /* DMB: We should get the defaults from the functionality
077: * Alan is working on. This is temporary.
078: * When that logic is finished, this block should
079: * probably just be deleted.
080: */
081: props.put(EnvProps.ASSEMBLER,
082: "org.apache.openejb.assembler.classic.Assembler");
083: props.put(EnvProps.CONFIGURATION_FACTORY,
084: "org.apache.openejb.config.ConfigurationFactory");
085: props.put(EnvProps.CONFIGURATION,
086: "conf/default.openejb.conf");
087:
088: props.putAll(SystemInstance.get().getProperties());
089:
090: props.putAll(env);
091:
092: org.apache.openejb.OpenEJB.init(props);
093:
094: } catch (org.apache.openejb.OpenEJBException e) {
095: throw new NamingException("Cannot initailize OpenEJB", e);
096: } catch (Exception e) {
097: throw new NamingException("Cannot initailize OpenEJB", e);
098: }
099: }
100:
101: }
|