ALMA Computing Group

LampCallbackImpl.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.LampCallbackImpl; 00023 00024 import java.util.logging.Level; 00025 00026 import alma.ACS.CBDescIn; 00027 import alma.ACS.CBdouble; 00028 import alma.ACS.CBdoubleHelper; 00029 import alma.ACSErr.CompletionHolder; 00030 import alma.ACS.Monitordouble; 00031 import alma.ACS.RWdouble; 00032 import alma.ACSErr.ACSException; 00033 import alma.ACSErrTypeJavaNative.wrappers.AcsJJavaLangEx; 00034 import alma.acs.component.ComponentImplBase; 00035 import alma.acs.exceptions.AcsJException; 00036 import alma.acsexmplLamp.Lamp; 00037 import alma.acsexmplLamp.LampHelper; 00038 import alma.demo.LampCallbackOperations; 00039 import alma.demo.LampUnavailable; 00040 00044 public class LampCallbackImpl extends ComponentImplBase implements LampCallbackOperations 00045 { 00046 private Lamp m_lamp; 00047 00048 private RWdouble m_brightness; 00049 00050 private CBdoubleLampCallback m_cb; 00051 private CBDescIn m_desc; 00052 00053 private Monitordouble m_monitor; 00054 00055 private static final String m_lampCurl = "LAMP1"; 00056 00057 00059 // Implementation of ComponentLifecycle 00061 00062 public void cleanUp() 00063 { 00064 m_logger.info("cleanUp() called..."); 00065 m_containerServices.releaseComponent(m_lampCurl); 00066 } 00067 00069 // Implementation of LampCallbackOperations 00071 00079 public double monitorLampBrightness() throws LampUnavailable 00080 { 00081 if (m_cb == null) 00082 { 00083 m_cb = new CBdoubleLampCallback(m_logger); 00084 m_logger.finer("CBdoubleLampCallback instantiated..."); 00085 } 00086 00087 CBdouble cbdouble; 00088 try 00089 { 00090 //test 00091 // CBdoubleHelper.narrow(m_containerServices.activateOffShoot(m_cb)); 00092 // m_containerServices.deactivateOffShoot(m_cb); 00093 00094 // note that m_cb may go through a cycle of activation/deactivation, see stopMonitor() 00095 cbdouble = CBdoubleHelper.narrow(m_containerServices.activateOffShoot(m_cb)); 00096 } 00097 catch (Exception e) 00098 { 00099 m_logger.log(Level.SEVERE, "failed to obtain the callback offshoot object.", e); 00100 throw new LampUnavailable(e.getMessage()); 00101 } 00102 00103 if (m_desc == null) 00104 { 00105 m_desc = new CBDescIn(); 00106 m_logger.info("m_desc instantiated..."); 00107 } 00108 00109 double brightness = 0; 00110 try 00111 { 00112 getLampBrightnessObject(); 00113 00114 CompletionHolder completionHolder = new CompletionHolder(); 00115 brightness = m_brightness.get_sync(completionHolder); 00116 00117 m_monitor = m_brightness.create_monitor(cbdouble, m_desc); 00118 m_logger.info("monitor instantiated..."); 00119 00120 // call every 10th second 00121 m_logger.info("prepares to trigger brightness every 10th second..."); 00122 m_monitor.set_timer_trigger(100000000); 00123 00124 m_logger.info("ready to trigger brightness..."); 00125 00126 } 00127 catch (Exception ex) 00128 { 00129 m_logger.log(Level.SEVERE, "ex in monitorLampBrightness impl", ex); 00130 throw new LampUnavailable(ex.getMessage()); 00131 } 00132 00133 return brightness; 00134 } 00135 00136 00142 public void stopMonitor() throws LampUnavailable 00143 { 00144 if (m_monitor != null) 00145 { 00146 try 00147 { 00148 m_monitor.destroy(); 00149 00150 m_containerServices.deactivateOffShoot(m_cb); 00151 m_logger.finer("deactivated CBdoubleLampCallback offshoot CORBA object."); 00152 } 00153 catch (Exception ex) 00154 { 00155 m_logger.log(Level.SEVERE, "ex in stopMonitor impl", ex); 00156 throw new LampUnavailable(ex.getMessage()); 00157 } 00158 } 00159 } 00160 00161 00162 00169 public void exceptionMethod() throws LampUnavailable 00170 { 00171 throw new LampUnavailable("use me for container tests..."); 00172 } 00173 00174 00175 00176 public void acsExceptionMethodVoid() throws ACSException 00177 { 00178 try 00179 { 00180 // imagine here's some impl code that throws AcsJExceptions 00181 acsJExceptionMethod(); 00182 } 00183 catch (AcsJException e) 00184 { 00185 // to the outside (CORBA) we must convert it 00186 ACSException e2 = e.getACSException(); 00187 throw e2; 00188 } 00189 } 00190 00191 00192 public double acsExceptionMethodDouble() throws ACSException 00193 { 00194 // this will throw the ex 00195 acsExceptionMethodVoid(); 00196 00197 // will never get here 00198 return -1.0; 00199 } 00200 00201 00202 private void acsJExceptionMethod() throws AcsJException 00203 { 00204 try 00205 { 00206 // imagine here's some code that throws exceptions 00207 Exception npe = new NullPointerException("mean NPE"); 00208 throw npe; 00209 } 00210 catch (Exception ex) 00211 { 00212 // wrap that npe with an acs ex and throw it on 00213 AcsJException acsEx = new AcsJJavaLangEx("low level ex", ex); 00214 throw acsEx; 00215 } 00216 } 00217 00218 00220 // other 00222 00228 public Lamp getLamp() throws LampUnavailable 00229 { 00230 if (m_lamp == null) 00231 { 00232 try 00233 { 00234 m_lamp = LampHelper.narrow(m_containerServices.getComponent(m_lampCurl)); 00235 } 00236 catch (Exception e) 00237 { 00238 String msg = "failed to obtain the component " + m_lampCurl; 00239 m_logger.log(Level.SEVERE, msg, e); 00240 throw new LampUnavailable(msg); 00241 } 00242 } 00243 return m_lamp; 00244 } 00245 00246 00252 public RWdouble getLampBrightnessObject() throws LampUnavailable 00253 { 00254 if (m_brightness == null) 00255 { 00256 getLamp(); 00257 try 00258 { 00259 m_brightness = m_lamp.brightness(); 00260 } 00261 catch (Exception e) 00262 { 00263 String msg = "failed to obtain the lamp's brightness object"; 00264 m_logger.log(Level.SEVERE, msg, e); 00265 throw new LampUnavailable(msg); 00266 } 00267 } 00268 return m_brightness; 00269 } 00270 } 00271