00001 /* 00002 * ServiceMediatorImpl.java 00003 * 00004 * Created on June 12, 2005, 3:58 PM 00005 * Created by Detro - 566/2145 00006 */ 00007 00008 package org.jaebi.server.service; 00009 00010 import org.jaebi.server.core.User; 00011 import org.jaebi.server.service.exception.BackEndHandlerInitException; 00012 import org.jaebi.server.service.exception.CommandCreationException; 00013 import org.jaebi.server.service.exception.CommandExecException; 00014 import org.jaebi.server.service.exception.ServiceMediatorExecException; 00015 import org.jaebi.server.service.exception.ServiceMediatorInitException; 00016 import org.jaebi.server.service.exception.XRequestCommandFactoryInitException; 00017 import org.jaebi.server.service.xml.JAXBMarshallerWrapper; 00018 import org.jaebi.server.service.xml.JAXBMarshallerWrapperImpl; 00019 import org.jaebi.server.service.xml.JAXBXResponseFactory; 00020 import org.jaebi.server.service.xml.exception.JAXBMarshallerWrapperInitException; 00021 import org.jaebi.server.service.xml.exception.JAXBMarshallerWrapperMarshallException; 00022 import org.jaebi.server.service.xml.exception.JAXBMarshallerWrapperUnmarshallException; 00023 import org.jaebi.server.service.xml.exception.JAXBXResponseFactoryBuildingException; 00024 import org.jaebi.server.service.xml.xrequest.Xrequest; 00025 import org.jaebi.server.service.xml.xresponse.Xresponse; 00026 00038 public class ServiceMediatorImpl implements ServiceMediator { 00039 private final BackEndHandler backEndHandler; 00040 private static JAXBMarshallerWrapper xrequestMarshWrap; 00041 private static JAXBMarshallerWrapper xresponseMarshWrap; 00042 private static XRequestCommandFactory commFactory; 00043 private static JAXBXResponseFactory xresponseFactory; 00044 private static final String exceptionPrefix = 00045 "[ServiceMediatorImpl Exception]: "; 00046 private final CommandValidator validator; 00047 00057 public ServiceMediatorImpl(String backEndPropertiesFilePath) 00058 throws ServiceMediatorInitException { 00059 00060 validator = new XRequestCommandValidator(); 00061 00062 try { 00063 backEndHandler = new BackEndHandlerImpl(backEndPropertiesFilePath); 00064 xrequestMarshWrap = new JAXBMarshallerWrapperImpl( 00065 "org.jaebi.server.service.xml.xrequest"); 00066 xresponseMarshWrap = new JAXBMarshallerWrapperImpl( 00067 "org.jaebi.server.service.xml.xresponse"); 00068 commFactory = new XRequestCommandFactory(backEndHandler); 00069 xresponseFactory = new JAXBXResponseFactory(); 00070 } catch ( BackEndHandlerInitException behie ) { 00071 throw new ServiceMediatorInitException( exceptionPrefix 00072 + behie ); 00073 } catch ( JAXBMarshallerWrapperInitException jmarinite) { 00074 throw new ServiceMediatorInitException( exceptionPrefix 00075 + jmarinite ); 00076 } catch ( XRequestCommandFactoryInitException xreqinite) { 00077 throw new ServiceMediatorInitException( exceptionPrefix 00078 + xreqinite ); 00079 } 00080 } 00081 00089 public ServiceMediatorImpl() 00090 throws ServiceMediatorInitException { 00091 00092 this("BackEndHandlerImpl.properties"); 00093 } 00094 00095 public Object executeService(User user, Object request) 00096 throws ServiceMediatorExecException { 00097 00098 Xrequest newReq = null; 00099 XRequestCommand newCommand = null; 00100 Xresponse newRes = null; 00101 00102 /* Controlla il tipo in INPUT e, se si tratta di String, 00103 * esegue l'unmarshalling in un Oggetto di tipo Xrequest */ 00104 if ( request instanceof String ) { 00105 try { 00106 newReq = (Xrequest)xrequestMarshWrap.unmarshall( 00107 (String)request); 00108 } catch ( JAXBMarshallerWrapperUnmarshallException unmarshe ) { 00109 throw new ServiceMediatorExecException( exceptionPrefix 00110 + unmarshe ); 00111 } 00112 } else { 00113 newReq = (Xrequest)request; 00114 } 00115 00116 /* Creazione nuovo XRequestCommand */ 00117 try { 00118 newCommand = commFactory.createCommand(newReq); 00119 } catch ( CommandCreationException cce) { 00120 throw new ServiceMediatorExecException( exceptionPrefix + cce ); 00121 } 00122 00123 /* Validazione Command */ 00124 if ( validator.validate(user, newCommand) ) { 00125 /* Esecuzione dell'XRequestCommand */ 00126 try { 00127 newRes = (Xresponse)newCommand.execute(); 00128 } catch ( CommandExecException cee ) { 00129 throw new ServiceMediatorExecException( exceptionPrefix + cee ); 00130 } 00131 } else { 00132 /* Creazione Xresponse di errore per mancata Validazione */ 00133 String errMessage = "This is not a Valid Request for this User."; 00134 try { 00135 newRes = (Xresponse)xresponseFactory.buildXresponse(false, errMessage ); 00136 } catch (JAXBXResponseFactoryBuildingException e) { 00137 throw new ServiceMediatorExecException( exceptionPrefix + e ); 00138 } 00139 } 00140 00141 return newRes; 00142 } 00143 00144 public String executeServiceToString(User user, Object request) 00145 throws ServiceMediatorExecException { 00146 00147 Xresponse xresponseResult = null; 00148 String result = null; 00149 00150 try { 00151 xresponseResult = (Xresponse)executeService(user, request); 00152 // xresponseMarshWrap.setFormattedMarshalling(true); //TODO Da problemi di parsing al client J2ME 00153 result = xresponseMarshWrap.marshall(xresponseResult); 00154 } catch ( ServiceMediatorExecException smee ) { 00155 throw new ServiceMediatorExecException( exceptionPrefix 00156 + smee ); 00157 } catch ( JAXBMarshallerWrapperMarshallException marshe ) { 00158 throw new ServiceMediatorExecException( exceptionPrefix 00159 + marshe ); 00160 } 00161 00162 return result; 00163 } 00164 }