001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.ca.helper.util;
019    
020    import java.math.BigInteger;
021    import java.util.Iterator;
022    import java.util.Set;
023    
024    import org.apache.geronimo.gbean.AbstractName;
025    import org.apache.geronimo.gbean.AbstractNameQuery;
026    import org.apache.geronimo.kernel.Kernel;
027    import org.apache.geronimo.kernel.KernelRegistry;
028    import org.apache.geronimo.management.geronimo.CertificateRequestStore;
029    import org.apache.geronimo.management.geronimo.CertificateStore;
030    import org.apache.geronimo.management.geronimo.SecureConnector;
031    
032    /**
033     * This class implements some methods used by the CA Helper Application.
034     *
035     * @version $Rev: 514091 $ $Date: 2007-03-03 01:26:39 -0500 (Sat, 03 Mar 2007) $
036     */
037    public class CAHelperUtils {
038        /**
039         * This method removes a certificate request stored in the CertificateRequestStore.
040         * @param csrId Id of the CSR to be removed.
041         * @param sNo Serial number of the certificate issued in response to the CSR to be removed.
042         */
043        public static void removeRequest(String csrId, BigInteger sNo) {
044            getCertificateRequestStore().removeRequestStatus(csrId, sNo);
045        }
046        
047        /**
048         * This method returns the CertificateRequestStore.
049         */
050        public static CertificateRequestStore getCertificateRequestStore() {
051            Kernel kernel = KernelRegistry.getSingleKernel();
052            
053            AbstractNameQuery certReqStoreQuery = new AbstractNameQuery(org.apache.geronimo.management.geronimo.CertificateRequestStore.class.getName());
054            Set set = kernel.listGBeans(certReqStoreQuery);
055            try {
056                CertificateRequestStore certReqStore = (CertificateRequestStore)kernel.getGBean((AbstractName)set.iterator().next());
057                return certReqStore;
058            } catch (Exception e) {
059                e.printStackTrace();
060            }
061            return null;
062        }
063        
064        /**
065         * This method returns the CertificateStore.
066         */
067        public static CertificateStore getCertificateStore() {
068            Kernel kernel = KernelRegistry.getSingleKernel();
069            
070            AbstractNameQuery certStoreQuery = new AbstractNameQuery(org.apache.geronimo.management.geronimo.CertificateStore.class.getName());
071            Set set = kernel.listGBeans(certStoreQuery);
072            try {
073                CertificateStore certStore = (CertificateStore)kernel.getGBean((AbstractName)set.iterator().next());
074                return certStore;
075            } catch(Exception e) {
076                e.printStackTrace();
077            }
078            return null;
079        }
080    
081        /**
082         * This method returns a port configured for HTTPS ClientAuthentication.
083         * 
084         * @return Port configured for HTTPS Client Authentication.
085         * @return -1 if no HTTPS Client Authentication Connector is configured.
086         */
087        public static int getHttpsClientAuthPort() {
088            Kernel kernel = KernelRegistry.getSingleKernel();
089            
090            AbstractNameQuery connectorQuery = new AbstractNameQuery(SecureConnector.class.getName());
091            Set set = kernel.listGBeans(connectorQuery);
092            for(Iterator itr = set.iterator(); itr.hasNext(); ){
093                try {
094                    SecureConnector connector = (SecureConnector)kernel.getGBean((AbstractName)itr.next());
095                    if(connector.isClientAuthRequired())
096                        return connector.getPort();
097                } catch(Exception e) {
098                    e.printStackTrace();
099                }
100            }
101            return -1;
102        }
103    }