All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softwarementors.extjs.djn.api.Registry Maven / Gradle / Ivy

/*
 * Copyright © 2008, 2012 Pedro Agulló Soliveres.
 * 
 * This file is part of DirectJNgine.
 *
 * DirectJNgine is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License.
 *
 * Commercial use is permitted to the extent that the code/component(s)
 * do NOT become part of another Open Source or Commercially developed
 * licensed development library or toolkit without explicit permission.
 *
 * DirectJNgine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with DirectJNgine.  If not, see .
 * 
 * This software uses the ExtJs library (http://extjs.com), which is 
 * distributed under the GPL v3 license (see http://extjs.com/license).
 */

package com.softwarementors.extjs.djn.api;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.softwarementors.extjs.djn.StringUtils;
import com.softwarementors.extjs.djn.config.GlobalConfiguration;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;

public class Registry {
  @NonNull /* package */ static final Logger logger = Logger.getLogger( Registry.class );
  
  @NonNull private Map apisByName = new HashMap();
  @NonNull private Map actionsByName = new HashMap();
  @NonNull private Map pollMethodsByName = new HashMap();
  @NonNull private Map sources = new HashMap();
  @NonNull private GlobalConfiguration globalConfiguration;
  @NonNull private static final Map gsonGenericParameterTypes = new HashMap();
  
  public static void registerParameterType( Method method, int parameter, Type type ) {
    assert method != null;
    assert parameter < method.getParameterTypes().length;
    assert type != null;
    
    Type[] methodParameterizedTypes = gsonGenericParameterTypes.get(method);
    if( methodParameterizedTypes == null ) {
      methodParameterizedTypes = new Type[method.getParameterTypes().length];
      gsonGenericParameterTypes.put( method, methodParameterizedTypes);
    }
    methodParameterizedTypes[parameter] = type;
  }
  
  public static Type[] getParameterTypes(Method m ) {
    Type[] result = gsonGenericParameterTypes.get(m);
    return result;
  }
  
  public Registry( GlobalConfiguration globalConfiguration ) {
    assert globalConfiguration != null;
    
    this.globalConfiguration = globalConfiguration;
  }

  public RegisteredApi addApi(String name, String apiFileName, String fullApiFileName, String apiNamespace, String actionsNamespace) {
    assert !StringUtils.isEmpty(name);
    assert !StringUtils.isEmpty(apiNamespace);
    assert !StringUtils.isEmpty(apiFileName);
    assert !StringUtils.isEmpty(fullApiFileName);
    assert actionsNamespace != null;
    assert !hasApi(name);
    
    RegisteredApi result = new RegisteredApi( this, name, apiFileName, fullApiFileName, apiNamespace, actionsNamespace);
    this.apisByName.put( name, result );    
    if( logger.isDebugEnabled() ) {
      logger.debug( "Registered new Api: " + name);
    }
    return result;
  }
  
  public boolean hasSource( String sourceName ) {
    assert !StringUtils.isEmpty(sourceName);
    
    return this.sources.containsKey(sourceName); 
  }
  
  public void addSource( String sourceName, String code) {
    assert !StringUtils.isEmpty(sourceName);
    assert code != null;
    assert !hasSource( sourceName );
    
    this.sources.put( sourceName, code);
  }
  
  public String getSource( String sourceName) {
    assert !StringUtils.isEmpty(sourceName);
    
    return this.sources.get(sourceName);
  }

  public RegisteredApi getApi( String api ) {
    assert !StringUtils.isEmpty(api);
    assert hasApi( api );
    
    return this.apisByName.get( api );
  }

  public boolean hasApi( String name ) {
    assert !StringUtils.isEmpty(name);
    
    return this.apisByName.containsKey(name);
  }
  
  public List getApis() {
    List result = new ArrayList(this.apisByName.values());
    return result;
  }
  
  public RegisteredAction getAction( String name ) {
    assert !StringUtils.isEmpty( name );
    assert hasAction( name );

    return this.actionsByName.get(name);
  }

  public boolean hasAction(String name) {
    assert !StringUtils.isEmpty( name );

    return this.actionsByName.containsKey(name);
  }

  public List getActions() {
    List result = new ArrayList(this.actionsByName.values());
    return result;
  }

  public boolean hasPollMethod( String eventName ) {
    assert !StringUtils.isEmpty(eventName);
    
    return this.pollMethodsByName.containsKey(eventName);
  }
  
  @CheckForNull public RegisteredPollMethod getPollMethod(String eventName) {
    assert !StringUtils.isEmpty(eventName);
    assert hasPollMethod(eventName);
    
    return this.pollMethodsByName.get(eventName);
  }

  public List getPollMethods() {
    List result = new ArrayList(this.pollMethodsByName.values());
    return result;
  }
  
  public GlobalConfiguration getGlobalConfiguration() {
    return this.globalConfiguration;
  }

  /* package */ void registerPollMethod(RegisteredPollMethod result) {
    assert result != null;
    assert !this.pollMethodsByName.containsKey(result.getName());
    
    this.pollMethodsByName.put( result.getName(), result );
  }

  /* package */ void registerAction(RegisteredAction result) {
    assert result != null;
    assert !this.actionsByName.containsKey(result.getName());
    
    this.actionsByName.put( result.getName(), result );
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy