001    /*
002     * 
003     * This file is part of DirectJNgine.
004     *
005     * DirectJNgine is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation, either version 3 of the License.
008     *
009     * Commercial use is permitted to the extent that the code/component(s)
010     * do NOT become part of another Open Source or Commercially developed
011     * licensed development library or toolkit without explicit permission.
012     *
013     * DirectJNgine is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
020     * 
021     * This software uses the ExtJs library (http://extjs.com), which is 
022     * distributed under the GPL v3 license (see http://extjs.com/license).
023     */
024    
025    package com.softwarementors.extjs.djn.api;
026    
027    import java.util.ArrayList;
028    import java.util.HashMap;
029    import java.util.List;
030    import java.util.Map;
031    
032    import org.apache.log4j.Logger;
033    
034    import com.softwarementors.extjs.djn.StringUtils;
035    import com.softwarementors.extjs.djn.config.GlobalConfiguration;
036    
037    public class Registry {
038      
039      /* package */ static Logger logger = Logger.getLogger( Registry.class );
040      
041      private Map<String, RegisteredApi> apisByName = new HashMap<String, RegisteredApi>();
042      private Map<String, RegisteredAction> actionsByName = new HashMap<String, RegisteredAction>();
043      private Map<String, RegisteredPollMethod> pollMethodsByName = new HashMap<String, RegisteredPollMethod>();
044      private GlobalConfiguration globalConfiguration;
045      
046      public Registry( GlobalConfiguration globalConfiguration ) {
047        assert globalConfiguration != null;
048        
049        this.globalConfiguration = globalConfiguration;
050      }
051    
052      public RegisteredApi addApi(String name, String fullApiFileName, String apiNamespace, String actionsNamespace) {
053        assert !StringUtils.isEmpty(name);
054        assert !StringUtils.isEmpty(apiNamespace);
055        assert !StringUtils.isEmpty(fullApiFileName);
056        assert actionsNamespace != null;
057        assert !hasApi(name);
058        
059        RegisteredApi result = new RegisteredApi( this, name, fullApiFileName, apiNamespace, actionsNamespace);
060        this.apisByName.put( name, result );    
061        if( logger.isDebugEnabled() ) {
062          logger.debug( "Registered new Api: " + name);
063        }
064        return result;
065      }
066    
067      public RegisteredApi getApi( String api ) {
068        assert !StringUtils.isEmpty(api);
069        assert hasApi( api );
070        
071        return this.apisByName.get( api );
072      }
073    
074      public boolean hasApi( String name ) {
075        assert !StringUtils.isEmpty(name);
076        
077        return this.apisByName.containsKey(name);
078      }
079      
080      public List<RegisteredApi> getApis() {
081        List<RegisteredApi> result = new ArrayList<RegisteredApi>(this.apisByName.values());
082        return result;
083      }
084      
085      public RegisteredAction getAction( String name ) {
086        assert !StringUtils.isEmpty( name );
087        assert hasAction( name );
088    
089        return this.actionsByName.get(name);
090      }
091    
092      public boolean hasAction(String name) {
093        assert !StringUtils.isEmpty( name );
094    
095        return this.actionsByName.containsKey(name);
096      }
097    
098      public List<RegisteredAction> getActions() {
099        List<RegisteredAction> result = new ArrayList<RegisteredAction>(this.actionsByName.values());
100        return result;
101      }
102    
103      public boolean hasPollMethod( String eventName ) {
104        assert !StringUtils.isEmpty(eventName);
105        
106        return this.pollMethodsByName.containsKey(eventName);
107      }
108      
109      public RegisteredPollMethod getPollMethod(String eventName) {
110        assert !StringUtils.isEmpty(eventName);
111        assert hasPollMethod(eventName);
112        
113        return this.pollMethodsByName.get(eventName);
114      }
115    
116      public List<RegisteredPollMethod> getPollMethods() {
117        List<RegisteredPollMethod> result = new ArrayList<RegisteredPollMethod>(this.pollMethodsByName.values());
118        return result;
119      }
120      
121      public GlobalConfiguration getGlobalConfiguration() {
122        return this.globalConfiguration;
123      }
124    
125      /* package */ void registerPollMethod(RegisteredPollMethod result) {
126        assert result != null;
127        assert !this.pollMethodsByName.containsKey(result.getName());
128        
129        this.pollMethodsByName.put( result.getName(), result );
130      }
131    
132      /* package */ void registerAction(RegisteredAction result) {
133        assert result != null;
134        assert !this.actionsByName.containsKey(result.getName());
135        
136        this.actionsByName.put( result.getName(), result );
137      }
138    
139    }