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 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.util.ArrayList; 029 import java.util.HashMap; 030 import java.util.List; 031 import java.util.Map; 032 033 import org.apache.log4j.Logger; 034 035 import com.softwarementors.extjs.djn.StringUtils; 036 import com.softwarementors.extjs.djn.config.GlobalConfiguration; 037 038 import edu.umd.cs.findbugs.annotations.CheckForNull; 039 import edu.umd.cs.findbugs.annotations.NonNull; 040 041 public class Registry { 042 043 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 052 public Registry( GlobalConfiguration globalConfiguration ) { 053 assert globalConfiguration != null; 054 055 this.globalConfiguration = globalConfiguration; 056 } 057 058 public RegisteredApi addApi(String name, String apiFileName, String fullApiFileName, String apiNamespace, String actionsNamespace) { 059 assert !StringUtils.isEmpty(name); 060 assert !StringUtils.isEmpty(apiNamespace); 061 assert !StringUtils.isEmpty(apiFileName); 062 assert !StringUtils.isEmpty(fullApiFileName); 063 assert actionsNamespace != null; 064 assert !hasApi(name); 065 066 RegisteredApi result = new RegisteredApi( this, name, apiFileName, fullApiFileName, apiNamespace, actionsNamespace); 067 this.apisByName.put( name, result ); 068 if( logger.isDebugEnabled() ) { 069 logger.debug( "Registered new Api: " + name); 070 } 071 return result; 072 } 073 074 public boolean hasSource( String sourceName ) { 075 assert !StringUtils.isEmpty(sourceName); 076 077 return this.sources.containsKey(sourceName); 078 } 079 080 public void addSource( String sourceName, String code) { 081 assert !StringUtils.isEmpty(sourceName); 082 assert code != null; 083 assert !hasSource( sourceName ); 084 085 this.sources.put( sourceName, code); 086 } 087 088 public String getSource( String sourceName) { 089 assert !StringUtils.isEmpty(sourceName); 090 091 return this.sources.get(sourceName); 092 } 093 094 public RegisteredApi getApi( String api ) { 095 assert !StringUtils.isEmpty(api); 096 assert hasApi( api ); 097 098 return this.apisByName.get( api ); 099 } 100 101 public boolean hasApi( String name ) { 102 assert !StringUtils.isEmpty(name); 103 104 return this.apisByName.containsKey(name); 105 } 106 107 public List<RegisteredApi> getApis() { 108 List<RegisteredApi> result = new ArrayList<RegisteredApi>(this.apisByName.values()); 109 return result; 110 } 111 112 public RegisteredAction getAction( String name ) { 113 assert !StringUtils.isEmpty( name ); 114 assert hasAction( name ); 115 116 return this.actionsByName.get(name); 117 } 118 119 public boolean hasAction(String name) { 120 assert !StringUtils.isEmpty( name ); 121 122 return this.actionsByName.containsKey(name); 123 } 124 125 public List<RegisteredAction> getActions() { 126 List<RegisteredAction> result = new ArrayList<RegisteredAction>(this.actionsByName.values()); 127 return result; 128 } 129 130 public boolean hasPollMethod( String eventName ) { 131 assert !StringUtils.isEmpty(eventName); 132 133 return this.pollMethodsByName.containsKey(eventName); 134 } 135 136 @CheckForNull public RegisteredPollMethod getPollMethod(String eventName) { 137 assert !StringUtils.isEmpty(eventName); 138 assert hasPollMethod(eventName); 139 140 return this.pollMethodsByName.get(eventName); 141 } 142 143 public List<RegisteredPollMethod> getPollMethods() { 144 List<RegisteredPollMethod> result = new ArrayList<RegisteredPollMethod>(this.pollMethodsByName.values()); 145 return result; 146 } 147 148 public GlobalConfiguration getGlobalConfiguration() { 149 return this.globalConfiguration; 150 } 151 152 /* package */ void registerPollMethod(RegisteredPollMethod result) { 153 assert result != null; 154 assert !this.pollMethodsByName.containsKey(result.getName()); 155 156 this.pollMethodsByName.put( result.getName(), result ); 157 } 158 159 /* package */ void registerAction(RegisteredAction result) { 160 assert result != null; 161 assert !this.actionsByName.containsKey(result.getName()); 162 163 this.actionsByName.put( result.getName(), result ); 164 } 165 166 }