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