1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 21 package org.apache.geronimo.security.jaspi; 22 23 import java.io.IOException; 24 import java.io.StringReader; 25 26 import javax.security.auth.callback.CallbackHandler; 27 import javax.security.auth.message.AuthException; 28 import javax.security.auth.message.config.AuthConfigFactory; 29 import javax.security.auth.message.config.AuthConfigProvider; 30 import javax.xml.bind.JAXBException; 31 import javax.xml.parsers.ParserConfigurationException; 32 import javax.xml.stream.XMLStreamException; 33 34 import org.apache.geronimo.components.jaspi.ClassLoaderLookup; 35 import org.apache.geronimo.components.jaspi.ConstantClassLoaderLookup; 36 import org.apache.geronimo.components.jaspi.model.JaspiUtil; 37 import org.apache.geronimo.components.jaspi.model.JaspiXmlUtil; 38 import org.apache.geronimo.components.jaspi.model.ServerAuthContextType; 39 import org.apache.geronimo.gbean.GBeanLifecycle; 40 import org.apache.geronimo.gbean.annotation.GBean; 41 import org.apache.geronimo.gbean.annotation.ParamAttribute; 42 import org.apache.geronimo.gbean.annotation.ParamSpecial; 43 import org.apache.geronimo.gbean.annotation.SpecialAttributeType; 44 import org.xml.sax.SAXException; 45 46 /** 47 * Holds a bit of xml configuring an AuthConfigProvider, [Client|Server][AuthConfig|AuthContext|AuthModule] 48 * and registers/unregisters it when start/stopped. 49 * 50 * @version $Rev: 720842 $ $Date: 2008-11-26 05:18:49 -0800 (Wed, 26 Nov 2008) $ 51 */ 52 53 @GBean 54 public class ServerAuthContextGBean implements GBeanLifecycle { 55 56 private final String registrationID; 57 58 public ServerAuthContextGBean( 59 @ParamAttribute(name = "config") String config, 60 @ParamSpecial(type = SpecialAttributeType.classLoader) ClassLoader classLoader) throws AuthException, JAXBException, IOException, ParserConfigurationException, SAXException, XMLStreamException { 61 ClassLoaderLookup classLoaderLookup = new ConstantClassLoaderLookup(classLoader); 62 63 AuthConfigFactory authConfigFactory = AuthConfigFactory.getFactory(); 64 ServerAuthContextType serverAuthContextType = JaspiXmlUtil.loadServerAuthContext(new StringReader(config)); 65 AuthConfigProvider authConfigProvider = JaspiUtil.wrapServerAuthContext(serverAuthContextType, true, classLoaderLookup); 66 registrationID = authConfigFactory.registerConfigProvider(authConfigProvider, serverAuthContextType.getMessageLayer(), serverAuthContextType.getAppContext(), null); 67 } 68 69 70 /** 71 * Starts the GBean. This informs the GBean that it is about to transition to the running state. 72 * 73 * @throws Exception if the target failed to start; this will cause a transition to the failed state 74 */ 75 public void doStart() throws Exception { 76 } 77 78 /** 79 * Stops the target. This informs the GBean that it is about to transition to the stopped state. 80 * 81 * @throws Exception if the target failed to stop; this will cause a transition to the failed state 82 */ 83 public void doStop() throws Exception { 84 AuthConfigFactory authConfigFactory = AuthConfigFactory.getFactory(); 85 authConfigFactory.removeRegistration(registrationID); 86 } 87 88 /** 89 * Fails the GBean. This informs the GBean that it is about to transition to the failed state. 90 */ 91 public void doFail() { 92 } 93 }