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, Method method) { 087 assert !StringUtils.isEmpty(name); 088 assert method != null; 089 assert RegisteredPollMethod.isValidPollMethod(method); 090 091 assert !getRegistry().hasPollMethod(name); 092 RegisteredPollMethod result = new RegisteredPollMethod( this, name, method ); 093 this.pollMethods.put( name, result ); 094 getRegistry().registerPollMethod( result ); 095 if( Registry.logger.isDebugEnabled()) { 096 Registry.logger.debug( " - Registered new Poll Method. Name: '" + name + "'. Java method: '" + result.getOwningClass() + "." + result.getMethod().getName() + "'" ); 097 } 098 return result; 099 } 100 101 public RegisteredPollMethod getPollMethod(String eventName) { 102 assert !StringUtils.isEmpty(eventName); 103 104 return this.pollMethods.get(eventName); 105 } 106 107 public List<RegisteredPollMethod> getPollMethods() { 108 return new ArrayList<RegisteredPollMethod>(this.pollMethods.values()); 109 } 110 111 public String getName() { 112 return this.name; 113 } 114 115 public String getFullApiFileName() { 116 return this.fullApiFileName; 117 } 118 119 public String getApiNamespace() { 120 return this.apiNamespace; 121 } 122 123 public String getActionsNamespace() { 124 return this.actionsNamespace; 125 } 126 127 }