View Javadoc

1   /*
2    * @(#)MyComponent.java   1.0
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10   */
11  package org.astrogrid.templateservice.templatecomponent ;
12  
13  import org.astrogrid.templateservice.* ;
14  import java.io.StringReader;
15  import javax.xml.parsers.DocumentBuilder;
16  import javax.xml.parsers.DocumentBuilderFactory;
17  
18  import org.apache.log4j.Logger;
19  import org.astrogrid.Configurator ;
20  import org.astrogrid.AstroGridException ;
21  import org.astrogrid.i18n.AstroGridMessage;
22  import org.w3c.dom.Document;
23  import org.xml.sax.InputSource;
24  
25  
26  /***
27   * The <code>MyComponent</code> class is 
28   *
29   * @author  Jeff Lusted
30   * @version 1.0 28-May-2003
31   * @since   AstroGrid 1.2
32   */
33  public class MyComponent {
34  
35  	/*** Compile-time switch used to turn tracing on/off. 
36  	  * Set this to false to eliminate all trace statements within the byte code.*/	 
37  	private static final boolean 
38  		TRACE_ENABLED = true ;
39  			
40  	private static final String
41  	    ASTROGRIDERROR_FAILED_TO_PARSE_REQUEST  = "AGMYCE00030",
42  	    ASTROGRIDERROR_ULTIMATE_REQUESTFAILURE  = "AGMYCE00040";
43  		
44  	/*** Log4J logger for this class. */    			    			   			
45  	private static Logger 
46  		logger = Logger.getLogger( MyComponent.class ) ;
47  	
48  	
49  	/***
50  	  *  
51  	  * Default constructor.
52  	  * <p>
53  	  * 
54  	  **/       	
55  	public MyComponent() {
56  		if( TRACE_ENABLED ) logger.debug( "MyComponent(): entry/exit") ;
57  	}
58  	
59  	
60  	/***
61  	  * <p> 
62  	  * Represents the mainline workflow argument for a typical AstroGrid component. 
63  	  * <p>
64  	  * 
65  	  * @param requestXML - The service request XML received as a String.
66  	  * @return A String. Maybe for testing purposes only if this service
67  	  * is presented as a one-way call.
68  	  * 
69  	  **/     
70      public String mainline( String requestXML ) { 	
71      	if( TRACE_ENABLED ) logger.debug( "mainline() entry") ;
72      	
73  		String
74  			response = null ;
75  			
76  		try { 
77  			// If the properties file is not loaded, we bail out...
78  			// Each component MUST be properly initialized! 
79              MYS.getInstance().checkPropertiesLoaded() ;
80    		
81      		// Parse the request...
82           	Document
83      		   doc = parseRequest( requestXML ) ;
84                 
85              // YOUR MAIN PROCESSING SHOULD GO HERE...
86              ;
87  				 
88  			// REPLACE THIS WITH A SUITABLE ACCESSOR...
89  			response = "place some suitable accessor here" ;	
90      	}
91      	catch( AstroGridException mex ) {
92  			AstroGridMessage
93  			    detailMessage = mex.getAstroGridMessage() ,  
94  				generalMessage = new AstroGridMessage( ASTROGRIDERROR_ULTIMATE_REQUESTFAILURE
95                                                       , this.getComponentName() ) ;
96  			logger.error( detailMessage.toString(), mex ) ;
97  			logger.error( generalMessage.toString() ) ;
98   				
99  			// If we are responding, we would format our error response here...
100 			if( response == null ) {
101 				response = generalMessage.toString() + "/n" + detailMessage.toString();
102 			}
103     	}
104     	finally {
105     		resourceCleanup() ;
106 			if( TRACE_ENABLED ) logger.debug( "runQuery() exit") ;
107     	}
108     	
109         return response ;   	
110     	 	
111     } // end mainline()
112 
113 
114     private Document parseRequest( String requestXML ) throws MyComponentException {  	
115 		if( TRACE_ENABLED ) logger.debug( "parseRequest() entry") ;
116 		
117 		Document 
118 		   doc = null;
119 		DocumentBuilderFactory 
120 		   factory = DocumentBuilderFactory.newInstance();
121 	    DocumentBuilder 
122 	       builder = null;
123 	       
124 		try {
125                     
126 		   factory.setValidating( Boolean.getBoolean( MYS.getProperty( MYS.DATASETAGENT_PARSER_VALIDATION
127 		                                                             , MYS.DATASETAGENT_CATEGORY )  )  ) ; 		
128 		   builder = factory.newDocumentBuilder();
129 		   logger.debug( requestXML ) ;
130 		   InputSource
131 		      jobSource = new InputSource( new StringReader( requestXML ) );
132            doc = builder.parse( jobSource );
133 		}
134 		catch ( Exception ex ) {
135 			AstroGridMessage
136 				message = new AstroGridMessage( ASTROGRIDERROR_FAILED_TO_PARSE_REQUEST
137                                               , this.getComponentName() ) ; 
138 			logger.error( message.toString(), ex ) ;
139 			throw new MyComponentException( message, ex );
140 		} 
141 		finally {
142 			if( TRACE_ENABLED ) logger.debug( "parseRequest() exit") ;	
143 		}
144 		
145 		return doc ;
146 
147     } // end parseRequest()
148     
149 
150     private void resourceCleanup() {   	
151 		if( TRACE_ENABLED ) logger.debug( "resourceCleanup() entry") ;
152 
153         try {
154             // YOUR CODE HERE...
155             ;
156         }
157         catch( Exception ex) {
158             ;
159         }
160         finally {
161             if( TRACE_ENABLED ) logger.debug( "resourceCleanup() exit") ;   
162         }
163 	
164     }
165 
166  
167 	public String getComponentName() { return Configurator.getClassName( MyComponent.class ) ; }
168 
169 
170 } // end of class