001    /*
002     * Copyright © 2008, 2012 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 Lesser 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 Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser 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.lang.reflect.Type;
030    import java.util.ArrayList;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    import org.apache.log4j.Logger;
036    
037    import com.softwarementors.extjs.djn.StringUtils;
038    import com.softwarementors.extjs.djn.config.GlobalConfiguration;
039    
040    import edu.umd.cs.findbugs.annotations.CheckForNull;
041    import edu.umd.cs.findbugs.annotations.NonNull;
042    
043    public class Registry {
044      @NonNull /* package */ static final Logger logger = Logger.getLogger( Registry.class );
045      
046      @NonNull private Map<String, RegisteredApi> apisByName = new HashMap<String, RegisteredApi>();
047      @NonNull private Map<String, RegisteredAction> actionsByName = new HashMap<String, RegisteredAction>();
048      @NonNull private Map<String, RegisteredPollMethod> pollMethodsByName = new HashMap<String, RegisteredPollMethod>();
049      @NonNull private Map<String, String> sources = new HashMap<String, String>();
050      @NonNull private GlobalConfiguration globalConfiguration;
051      @NonNull private static final Map<Method, Type[]> gsonGenericParameterTypes = new HashMap<Method, Type[]>();
052      
053      public static void registerParameterType( Method method, int parameter, Type type ) {
054        assert method != null;
055        assert parameter < method.getParameterTypes().length;
056        assert type != null;
057        
058        Type[] methodParameterizedTypes = gsonGenericParameterTypes.get(method);
059        if( methodParameterizedTypes == null ) {
060          methodParameterizedTypes = new Type[method.getParameterTypes().length];
061          gsonGenericParameterTypes.put( method, methodParameterizedTypes);
062        }
063        methodParameterizedTypes[parameter] = type;
064      }
065      
066      public static Type[] getParameterTypes(Method m ) {
067        Type[] result = gsonGenericParameterTypes.get(m);
068        return result;
069      }
070      
071      public Registry( GlobalConfiguration globalConfiguration ) {
072        assert globalConfiguration != null;
073        
074        this.globalConfiguration = globalConfiguration;
075      }
076    
077      public RegisteredApi addApi(String name, String apiFileName, String fullApiFileName, String apiNamespace, String actionsNamespace) {
078        assert !StringUtils.isEmpty(name);
079        assert !StringUtils.isEmpty(apiNamespace);
080        assert !StringUtils.isEmpty(apiFileName);
081        assert !StringUtils.isEmpty(fullApiFileName);
082        assert actionsNamespace != null;
083        assert !hasApi(name);
084        
085        RegisteredApi result = new RegisteredApi( this, name, apiFileName, fullApiFileName, apiNamespace, actionsNamespace);
086        this.apisByName.put( name, result );    
087        if( logger.isDebugEnabled() ) {
088          logger.debug( "Registered new Api: " + name);
089        }
090        return result;
091      }
092      
093      public boolean hasSource( String sourceName ) {
094        assert !StringUtils.isEmpty(sourceName);
095        
096        return this.sources.containsKey(sourceName); 
097      }
098      
099      public void addSource( String sourceName, String code) {
100        assert !StringUtils.isEmpty(sourceName);
101        assert code != null;
102        assert !hasSource( sourceName );
103        
104        this.sources.put( sourceName, code);
105      }
106      
107      public String getSource( String sourceName) {
108        assert !StringUtils.isEmpty(sourceName);
109        
110        return this.sources.get(sourceName);
111      }
112    
113      public RegisteredApi getApi( String api ) {
114        assert !StringUtils.isEmpty(api);
115        assert hasApi( api );
116        
117        return this.apisByName.get( api );
118      }
119    
120      public boolean hasApi( String name ) {
121        assert !StringUtils.isEmpty(name);
122        
123        return this.apisByName.containsKey(name);
124      }
125      
126      public List<RegisteredApi> getApis() {
127        List<RegisteredApi> result = new ArrayList<RegisteredApi>(this.apisByName.values());
128        return result;
129      }
130      
131      public RegisteredAction getAction( String name ) {
132        assert !StringUtils.isEmpty( name );
133        assert hasAction( name );
134    
135        return this.actionsByName.get(name);
136      }
137    
138      public boolean hasAction(String name) {
139        assert !StringUtils.isEmpty( name );
140    
141        return this.actionsByName.containsKey(name);
142      }
143    
144      public List<RegisteredAction> getActions() {
145        List<RegisteredAction> result = new ArrayList<RegisteredAction>(this.actionsByName.values());
146        return result;
147      }
148    
149      public boolean hasPollMethod( String eventName ) {
150        assert !StringUtils.isEmpty(eventName);
151        
152        return this.pollMethodsByName.containsKey(eventName);
153      }
154      
155      @CheckForNull public RegisteredPollMethod getPollMethod(String eventName) {
156        assert !StringUtils.isEmpty(eventName);
157        assert hasPollMethod(eventName);
158        
159        return this.pollMethodsByName.get(eventName);
160      }
161    
162      public List<RegisteredPollMethod> getPollMethods() {
163        List<RegisteredPollMethod> result = new ArrayList<RegisteredPollMethod>(this.pollMethodsByName.values());
164        return result;
165      }
166      
167      public GlobalConfiguration getGlobalConfiguration() {
168        return this.globalConfiguration;
169      }
170    
171      /* package */ void registerPollMethod(RegisteredPollMethod result) {
172        assert result != null;
173        assert !this.pollMethodsByName.containsKey(result.getName());
174        
175        this.pollMethodsByName.put( result.getName(), result );
176      }
177    
178      /* package */ void registerAction(RegisteredAction result) {
179        assert result != null;
180        assert !this.actionsByName.containsKey(result.getName());
181        
182        this.actionsByName.put( result.getName(), result );
183      }
184    
185    }