ALMA Computing Group

XmlComponentHelper.java

Go to the documentation of this file.
00001 /* 00002 * ALMA - Atacama Large Millimiter Array 00003 * (c) European Southern Observatory, 2002 00004 * Copyright by ESO (in the framework of the ALMA collaboration), 00005 * All rights reserved 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00020 * MA 02111-1307 USA 00021 */ 00022 package alma.demo.XmlComponentImpl; 00023 00024 import java.util.logging.Level; 00025 import java.util.logging.Logger; 00026 00027 import org.omg.PortableServer.Servant; 00028 00029 import alma.ACS.ACSComponentOperations; 00030 import alma.ACS.ComponentStates; 00031 import alma.JContExmplErrTypeTest.XmlComponentErrorEx; 00032 import alma.JavaContainerError.wrappers.AcsJContainerServicesEx; 00033 import alma.JavaContainerError.wrappers.AcsJJavaComponentHelperEx; 00034 import alma.acs.component.ComponentLifecycle; 00035 import alma.acs.container.ComponentHelper; 00036 import alma.acs.entityutil.EntityDeserializer; 00037 import alma.acs.entityutil.EntityException; 00038 import alma.acs.entityutil.EntitySerializer; 00039 import alma.demo.ObsProjectTree; 00040 import alma.demo.ObsProjectTreeJ; 00041 import alma.demo.SchedBlockHolder; 00042 import alma.demo.XmlComponentJ; 00043 import alma.demo.XmlComponentOperations; 00044 import alma.demo.XmlComponentPOATie; 00045 import alma.maciErrType.wrappers.AcsJComponentCreationEx; 00046 import alma.xmlentity.XmlEntityStruct; 00047 import alma.xmlentity.XmlEntityStructHolder; 00048 import alma.xmljbind.test.obsproposal.ObsProposal; 00049 import alma.xmljbind.test.schedblock.SchedBlock; 00050 00051 00052 00065 public class XmlComponentHelper extends ComponentHelper 00066 { 00067 00068 00072 public XmlComponentHelper(Logger containerLogger) 00073 { 00074 super(containerLogger); 00075 } 00076 00080 protected ComponentLifecycle _createComponentImpl() 00081 { 00082 return new XmlComponentImpl(); 00083 } 00084 00088 protected Class<? extends Servant> _getPOATieClass() 00089 { 00090 return XmlComponentPOATie.class; 00091 } 00092 00096 protected Class<? extends ACSComponentOperations> _getOperationsInterface() 00097 { 00098 return XmlComponentOperations.class; 00099 } 00100 00101 00105 protected Class<?> getInternalInterface() 00106 { 00107 return XmlComponentJ.class; 00108 } 00109 00110 00114 protected Object _getInterfaceTranslator(Object defaultInterfaceTranslator) throws AcsJJavaComponentHelperEx { 00115 XmlComponentJ impl = null; 00116 XmlComponentOperations opDelegate = null; 00117 try { 00118 impl = (XmlComponentJ) getComponentImpl(); 00119 } catch (AcsJComponentCreationEx e) { 00120 throw new AcsJJavaComponentHelperEx(e); 00121 } 00122 opDelegate = (XmlComponentOperations) defaultInterfaceTranslator; 00123 return new IFTranslator(impl, opDelegate, getComponentLogger()); 00124 } 00125 00126 protected String[] _getComponentMethodsExcludedFromInvocationLogging() { 00127 return new String[] {"dumbMethod"}; 00128 } 00129 00157 private static class IFTranslator implements XmlComponentOperations 00158 { 00159 private boolean m_useDefaultTranslator = true; // toggle for dev testing 00160 00161 private XmlComponentJ m_componentImpl; 00162 private XmlComponentOperations m_defaultTranslator; 00163 private EntitySerializer m_entSer; 00164 00165 private Logger m_ifTransLogger; 00166 00167 IFTranslator(XmlComponentJ componentImpl, XmlComponentOperations defaultTranslator, Logger logger) 00168 { 00169 m_componentImpl = componentImpl; 00170 m_defaultTranslator = defaultTranslator; 00171 m_ifTransLogger = logger; 00172 m_entSer = EntitySerializer.getEntitySerializer(logger); 00173 } 00174 00175 public ComponentStates componentState() 00176 { 00177 return m_componentImpl.componentState(); 00178 } 00179 00180 public String name() 00181 { 00182 return m_componentImpl.name(); 00183 } 00184 00185 00189 public XmlEntityStruct createObsProposal() 00190 { 00191 XmlEntityStruct entStruct = null; 00192 if (m_useDefaultTranslator) 00193 { 00194 entStruct = m_defaultTranslator.createObsProposal(); 00195 } 00196 else 00197 { 00198 // call directly into component, do custom translations 00199 ObsProposal opsProp = m_componentImpl.createObsProposal(); 00200 try 00201 { 00202 entStruct = m_entSer.serializeEntity(opsProp, opsProp.getObsProposalEntity()); 00203 } 00204 catch (EntityException e) 00205 { 00206 e.printStackTrace(); 00207 // signature of createObsProposal does not foresee marshalling problems... 00208 throw new RuntimeException(e); 00209 } 00210 } 00211 return entStruct; 00212 } 00213 00217 public int dumbMethod(String somevalue) 00218 { 00219 return m_componentImpl.dumbMethod(somevalue); 00220 } 00221 00225 public XmlEntityStruct[] getAllSchedBlocks() 00226 { 00227 XmlEntityStruct[] entStructs = null; 00228 00229 if (m_useDefaultTranslator) 00230 { 00231 entStructs = m_defaultTranslator.getAllSchedBlocks(); 00232 } 00233 else 00234 { 00235 SchedBlock[] schedBlocks = m_componentImpl.getAllSchedBlocks(); 00236 00237 if (schedBlocks == null) 00238 { 00239 return new XmlEntityStruct[0]; 00240 } 00241 00242 entStructs = new XmlEntityStruct[schedBlocks.length]; 00243 try 00244 { 00245 for (int i = 0; i < schedBlocks.length; i++) 00246 { 00247 entStructs[i] = m_entSer.serializeEntity(schedBlocks[i], schedBlocks[i].getSchedBlockEntity()); 00248 } 00249 } 00250 catch (Exception ex) 00251 { 00252 ex.printStackTrace(); // todo: add exception to IDL and throw it here 00253 } 00254 } 00255 00256 return entStructs; 00257 } 00258 00259 00263 public void addNewSchedBlocks(XmlEntityStruct[] newSchedBlocks) 00264 { 00265 String msg = "will add " + newSchedBlocks.length + " SchedBlock(s):\n"; 00266 for (int i = 0; i < newSchedBlocks.length; i++) { 00267 msg += newSchedBlocks[i].xmlString + "\n"; 00268 } 00269 m_ifTransLogger.info(msg); 00270 00271 if (m_useDefaultTranslator) 00272 { 00273 m_defaultTranslator.addNewSchedBlocks(newSchedBlocks); 00274 } 00275 else 00276 { 00277 m_ifTransLogger.log(Level.WARNING, "bummer, manual translation of newSchedBlocks array not implemented."); 00278 } 00279 } 00280 00284 public XmlEntityStruct getBestSchedBlock() 00285 { 00286 XmlEntityStruct entStruct = null; 00287 00288 if (m_useDefaultTranslator) 00289 { 00290 entStruct = m_defaultTranslator.getBestSchedBlock(); 00291 } 00292 else 00293 { 00294 try 00295 { 00296 SchedBlock sb = m_componentImpl.getBestSchedBlock(); 00297 entStruct = m_entSer.serializeEntity(sb, sb.getSchedBlockEntity()); 00298 } 00299 catch (Exception e) 00300 { 00301 e.printStackTrace(); // todo: add exception to IDL and throw it here 00302 } 00303 } 00304 00305 return entStruct; 00306 } 00307 00308 00312 public void xmlInOutMethod(XmlEntityStruct opsPropIn, XmlEntityStructHolder schedBlockOut) 00313 { 00314 if (m_useDefaultTranslator) 00315 { 00316 m_defaultTranslator.xmlInOutMethod(opsPropIn, schedBlockOut); 00317 } 00318 else 00319 { 00320 try 00321 { 00322 EntityDeserializer entdes = EntityDeserializer.getEntityDeserializer(m_ifTransLogger); 00323 ObsProposal obsProp = (ObsProposal) entdes.deserializeEntity(opsPropIn, ObsProposal.class); 00324 SchedBlockHolder sbh = new SchedBlockHolder(); 00325 00326 m_componentImpl.xmlInOutMethod(obsProp, sbh); 00327 00328 SchedBlock schedBlock = sbh.value; 00329 XmlEntityStruct schedStruct = m_entSer.serializeEntity(schedBlock, schedBlock.getSchedBlockEntity()); 00330 schedBlockOut.value = schedStruct; 00331 00332 } 00333 catch (Exception ex) 00334 { 00335 ex.printStackTrace(); // todo: add exception to IDL and throw it here 00336 } 00337 } 00338 } 00339 00340 00344 public ObsProjectTree getEntireTreeInAStruct() 00345 { 00346 ObsProjectTree struct = null; 00347 00348 if (m_useDefaultTranslator) 00349 { 00350 struct = m_defaultTranslator.getEntireTreeInAStruct(); 00351 } 00352 else 00353 { 00354 ObsProjectTreeJ structJ = m_componentImpl.getEntireTreeInAStruct(); 00355 00356 struct = new ObsProjectTree(); 00357 try 00358 { 00359 struct.prop = m_entSer.serializeEntity(structJ.prop, structJ.prop.getObsProposalEntity()); 00360 00361 for (int i = 0; i < structJ.schedBlocks.length; i++) 00362 { 00363 SchedBlock sb = structJ.schedBlocks[i]; 00364 XmlEntityStruct xes = m_entSer.serializeEntity(sb, sb.getSchedBlockEntity()); 00365 struct.schedBlocks[i] = xes; 00366 } 00367 } 00368 catch (EntityException e) 00369 { 00370 m_ifTransLogger.log(Level.WARNING, "exception in manual translation of entities.", e); 00371 } 00372 } 00373 return struct; 00374 } 00375 00376 00380 public String sayHello() 00381 { 00382 return m_componentImpl.sayHello(); 00383 } 00384 00385 00386 /* (non-Javadoc) 00387 * @see alma.demo.XmlComponentOperations#exceptionMethod() 00388 */ 00389 public void exceptionMethod() throws XmlComponentErrorEx 00390 { 00391 m_defaultTranslator.exceptionMethod(); 00392 } 00393 00394 } 00395 } 00396 00397