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.router.processor; 027 028 import org.apache.log4j.Logger; 029 030 import com.google.gson.Gson; 031 import com.google.gson.GsonBuilder; 032 import com.google.gson.JsonParseException; 033 import com.softwarementors.extjs.djn.api.Registry; 034 import com.softwarementors.extjs.djn.config.GlobalConfiguration; 035 import com.softwarementors.extjs.djn.gson.GsonBuilderConfigurator; 036 import com.softwarementors.extjs.djn.gson.GsonBuilderConfiguratorException; 037 import com.softwarementors.extjs.djn.gson.JsonException; 038 import com.softwarementors.extjs.djn.router.dispatcher.Dispatcher; 039 040 import edu.umd.cs.findbugs.annotations.CheckForNull; 041 import edu.umd.cs.findbugs.annotations.NonNull; 042 043 public abstract class RequestProcessorBase { 044 045 @NonNull private static Logger logger = Logger.getLogger(RequestProcessorBase.class); 046 @NonNull private Dispatcher dispatcher; 047 @NonNull private Registry registry; 048 @NonNull private GlobalConfiguration globalConfiguration; 049 @CheckForNull private Gson gson; 050 051 protected GlobalConfiguration getGlobalConfiguration() { 052 return this.globalConfiguration; 053 } 054 055 protected RequestProcessorBase( Registry registry, Dispatcher dispatcher, GlobalConfiguration globalConfiguration ) { 056 assert registry != null; 057 assert dispatcher != null; 058 assert globalConfiguration != null; 059 060 this.dispatcher = dispatcher; 061 this.globalConfiguration = globalConfiguration; 062 this.registry = registry; 063 } 064 065 protected Registry getRegistry() { 066 return this.registry; 067 } 068 069 protected Dispatcher getDispatcher() { 070 return this.dispatcher; 071 } 072 073 protected boolean getDebug() { 074 return this.globalConfiguration.getDebug(); 075 } 076 077 // ************************************************************ 078 // * JSON support 079 // ************************************************************ 080 protected void appendIndividualResponseJsonString(ResponseData response, StringBuilder result) { 081 assert response != null; 082 assert result != null; 083 084 try { 085 getGson().toJson( response, result ); 086 } 087 catch( JsonParseException ex ) { 088 JsonException.forFailedConversionFromResponseToJson(response, ex); 089 } 090 } 091 protected Gson getGson() { 092 if( this.gson == null ) { 093 GsonBuilder builder = new GsonBuilder(); 094 createGsonBuilderConfigurator().configure( builder, getGlobalConfiguration()); 095 this.gson = builder.create(); 096 } 097 098 return this.gson; 099 } 100 101 private GsonBuilderConfigurator createGsonBuilderConfigurator() { 102 Class<? extends GsonBuilderConfigurator> configuratorClass = getGlobalConfiguration().getGsonBuilderConfiguratorClass(); 103 try { 104 return configuratorClass.newInstance(); 105 } 106 catch (InstantiationException e) { 107 GsonBuilderConfiguratorException ex = GsonBuilderConfiguratorException.forUnableToInstantiateGsonBuilder(configuratorClass, e); 108 logger.fatal( ex.getMessage(), ex); 109 throw ex; 110 } 111 catch (IllegalAccessException e) { 112 GsonBuilderConfiguratorException ex = GsonBuilderConfiguratorException.forUnableToInstantiateGsonBuilder(configuratorClass, e); 113 logger.fatal( ex.getMessage(), ex); 114 throw ex; 115 } 116 } 117 118 }