001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.keymanager;
019    
020    import java.net.MalformedURLException;
021    import java.net.URL;
022    import java.util.ArrayList;
023    import java.util.Collection;
024    import java.util.Collections;
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import org.cumulus4j.keystore.KeyStore;
029    
030    /**
031     * <p>
032     * Manager for {@link AppServer}s.
033     * </p>
034     * <p>
035     * This is the actual key-manager-entry-point, as for every {@link KeyStore}, there can be many <code>AppServer</code>s in use.
036     * An instance of <code>AppServerManager</code> is bound to an instance of KeyStore (i.e. they are in a 1-1-relationship).
037     * </p>
038     * <p>
039     * This is not API! Use the classes and interfaces provided by <code>org.cumulus4j.keymanager.api</code> instead.
040     * </p>
041     *
042     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
043     */
044    public class AppServerManager
045    {
046            private KeyStore keyStore;
047    
048            private Map<String, AppServer> appServerID2appServer = new HashMap<String, AppServer>();
049            private Collection<AppServer> appServers = null;
050    
051            public AppServerManager(KeyStore keyStore)
052            {
053                    if (keyStore == null)
054                            throw new IllegalArgumentException("keyStore == null");
055    
056                    this.keyStore = keyStore;
057            }
058    
059            public KeyStore getKeyStore() {
060                    return keyStore;
061            }
062    
063            public synchronized AppServer getAppServerForAppServerID(String appServerID)
064            {
065                    if (appServerID == null)
066                            throw new IllegalArgumentException("appServerID == null");
067    
068                    return appServerID2appServer.get(appServerID);
069            }
070    
071            public synchronized void putAppServer(AppServer appServer)
072            {
073                    if (appServer == null)
074                            throw new IllegalArgumentException("appServer == null");
075    
076                    if (this != appServer.getAppServerManager())
077                            throw new IllegalArgumentException("appServer.appServerManager != this");
078    
079                    if (appServer.getAppServerBaseURL() == null)
080                            throw new IllegalArgumentException("appServer.appServerBaseURL == null");
081    
082                    URL url;
083                    try {
084                            url = new URL(appServer.getAppServerBaseURL());
085                    } catch (MalformedURLException e) {
086                            throw new IllegalArgumentException("appServer.appServerBaseURL is not a valid URL: " + e, e);
087                    }
088    
089                    if (appServer.getAppServerID() == null) {
090                            AppServer oldAppServer;
091                            String id;
092                            int index = -1;
093                            do {
094                                    id = url.getHost();
095                                    if (url.getPort() < 0) {
096                                            if (url.getDefaultPort() >= 0)
097                                                    id += '-' + url.getDefaultPort();
098                                    }
099                                    else
100                                            id += '-' + url.getPort();
101    
102                                    if (++index > 0)
103                                            id += '-' + index;
104    
105                                    oldAppServer = this.getAppServerForAppServerID(id);
106                            } while (oldAppServer != null && !appServer.getAppServerBaseURL().equals(oldAppServer.getAppServerBaseURL()));
107    
108                            appServer.setAppServerID(id);
109                    }
110    
111                    appServerID2appServer.put(appServer.getAppServerID(), appServer);
112                    appServers = null;
113            }
114    
115            public synchronized Collection<AppServer> getAppServers() {
116                    if (appServers == null) {
117                            appServers = Collections.unmodifiableCollection(new ArrayList<AppServer>(appServerID2appServer.values()));
118                    }
119                    return appServers;
120            }
121    
122            public synchronized void removeAppServer(String appServerID) {
123                    appServerID2appServer.remove(appServerID);
124            }
125    }