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.servicemix.common.security;
018    
019    import java.security.PrivateKey;
020    import java.security.GeneralSecurityException;
021    import java.security.cert.Certificate;
022    import java.lang.reflect.InvocationHandler;
023    import java.lang.reflect.Method;
024    
025    import javax.net.ssl.KeyManager;
026    import javax.net.ssl.TrustManager;
027    
028    /**
029     * Based on http://svn.apache.org/repos/asf/geronimo/trunk/modules/management/
030     *              src/java/org/apache/geronimo/management/geronimo/KeystoreInstance.java
031     *
032     * @version $Rev: $ $Date: $
033     */
034    public interface KeystoreInstance {
035    
036        String getName();
037    
038        String[] listPrivateKeys();
039    
040        String[] listTrustCertificates();
041    
042        Certificate getCertificate(String alias);
043    
044        String getCertificateAlias(Certificate cert);
045    
046        Certificate[] getCertificateChain(String alias);
047    
048        PrivateKey getPrivateKey(String alias);
049    
050        boolean isKeystoreLocked();
051    
052        boolean isKeyLocked(String keyAlias);
053    
054        KeyManager[] getKeyManager(String algorithm, String keyAlias) throws GeneralSecurityException;
055    
056        TrustManager[] getTrustManager(String algorithm) throws GeneralSecurityException;
057    
058        public static final class Proxy {
059            public static KeystoreInstance create(final Object target) {
060                return (KeystoreInstance) java.lang.reflect.Proxy.newProxyInstance(KeystoreInstance.class.getClassLoader(),
061                                                                                   new Class[] { KeystoreInstance.class },
062                                                                                   new InvocationHandler() {
063                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
064                        Object o = target.getClass().getMethod(method.getName(), method.getParameterTypes()).invoke(target, args);
065                        return o;
066                    }
067                });
068            }
069        }
070    }