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.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 /************************ Per-thread */ 050 @CheckForNull private static Gson gson; // = new ThreadLocal<Gson>(); 051 /************************ Per-thread: end */ 052 053 protected GlobalConfiguration getGlobalConfiguration() { 054 return this.globalConfiguration; 055 } 056 057 protected RequestProcessorBase( Registry registry, Dispatcher dispatcher, GlobalConfiguration globalConfiguration ) { 058 assert registry != null; 059 assert dispatcher != null; 060 assert globalConfiguration != null; 061 062 this.dispatcher = dispatcher; 063 this.globalConfiguration = globalConfiguration; 064 this.registry = registry; 065 } 066 067 protected Registry getRegistry() { 068 return this.registry; 069 } 070 071 protected Dispatcher getDispatcher() { 072 return this.dispatcher; 073 } 074 075 protected boolean getDebug() { 076 return this.globalConfiguration.getDebug(); 077 } 078 079 // ************************************************************ 080 // * JSON support 081 // ************************************************************ 082 protected void appendIndividualResponseJsonString(ResponseData response, StringBuilder result) { 083 assert response != null; 084 assert result != null; 085 086 try { 087 getGson().toJson( response, result ); 088 } 089 catch( JsonParseException ex ) { 090 JsonException.forFailedConversionFromResponseToJson(response, ex); 091 } 092 } 093 094 protected synchronized Gson getGson() { 095 /****************************** Per-thread */ 096 /* 097 Gson result = this.gson.get(); 098 if( result == null ) { 099 GsonBuilder builder = new GsonBuilder(); 100 createGsonBuilderConfigurator().configure( builder, getGlobalConfiguration()); 101 result = builder.create(); 102 this.gson.set( result ); 103 } 104 return result; 105 */ 106 /****************************** Per-thread: end */ 107 108 /****************************** Per-instance */ 109 if( gson == null ) { 110 GsonBuilder builder = new GsonBuilder(); 111 createGsonBuilderConfigurator().configure( builder, getGlobalConfiguration()); 112 gson = builder.create(); 113 } 114 return gson; 115 /******************************* Per-intance: end */ 116 } 117 118 private GsonBuilderConfigurator createGsonBuilderConfigurator() { 119 Class<? extends GsonBuilderConfigurator> configuratorClass = getGlobalConfiguration().getGsonBuilderConfiguratorClass(); 120 try { 121 return configuratorClass.newInstance(); 122 } 123 catch (InstantiationException e) { 124 GsonBuilderConfiguratorException ex = GsonBuilderConfiguratorException.forUnableToInstantiateGsonBuilder(configuratorClass, e); 125 logger.fatal( ex.getMessage(), ex); 126 throw ex; 127 } 128 catch (IllegalAccessException e) { 129 GsonBuilderConfiguratorException ex = GsonBuilderConfiguratorException.forUnableToInstantiateGsonBuilder(configuratorClass, e); 130 logger.fatal( ex.getMessage(), ex); 131 throw ex; 132 } 133 } 134 135 }