KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > component > example_im > Main


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.avalon.excalibur.component.example_im;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24
25 import org.apache.avalon.excalibur.component.ExcaliburComponentManagerCreator;
26 import org.apache.avalon.framework.service.ServiceManager;
27
28 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
29
30
31 /**
32  * This example application loads a component which publishes a series
33  * of Instruments. An InstrumentManager is created to collect and
34  * manage the Instrument data. And an Altrmi based InstrumentManagerInterface
35  * is registered. A client may connect to InstrumentManager later.
36  * <p>
37  * Note, this code ignores exceptions to keep the code simple.
38  *
39  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
40  * @version CVS $Revision: 1.1 $ $Date: 2004/03/17 13:22:38 $
41  * @since 4.1
42  */

43 public class Main
44 {
45     private static ExcaliburComponentManagerCreator m_componentManagerCreator;
46
47     /*---------------------------------------------------------------
48      * Constructors
49      *-------------------------------------------------------------*/

50     private Main()
51     {
52     }
53
54     /*---------------------------------------------------------------
55      * Main method
56      *-------------------------------------------------------------*/

57     /**
58      * All of the guts of this example exist in the main method.
59      */

60     public static void main( String JavaDoc[] args )
61         throws Exception JavaDoc
62     {
63         System.out.println( "Running the InstrumentManager Example Application" );
64
65         // Create the ComponentManager using the ExcaliburComponentManagerCreator
66
// utility class. See the contents of that class if you wish to do the
67
// initialization yourself.
68
m_componentManagerCreator = new ExcaliburComponentManagerCreator( null,
69             new File JavaDoc( "../conf/logkit.xml" ), new File JavaDoc( "../conf/roles.xml" ),
70             new File JavaDoc( "../conf/components.xml" ), new File JavaDoc( "../conf/instrument.xml" ) );
71
72         // Get a reference to the service manager
73
ServiceManager serviceManager = m_componentManagerCreator.getServiceManager();
74
75         // Get a reference to the example component.
76
ExampleInstrumentable instrumentable =
77             (ExampleInstrumentable)serviceManager.lookup( ExampleInstrumentable.ROLE );
78         try
79         {
80             boolean quit = false;
81             while( !quit )
82             {
83                 System.out.println( "Enter the number of times that exampleAction should be "
84                                     + "called, or 'q' to quit." );
85                 BufferedReader JavaDoc in = new BufferedReader JavaDoc( new InputStreamReader JavaDoc( System.in ) );
86                 System.out.print( " : " );
87                 String JavaDoc cntStr = in.readLine();
88
89                 // Can get a null if CTRL-C is hit.
90
if( ( cntStr == null ) || ( cntStr.equalsIgnoreCase( "q" ) ) )
91                 {
92                     quit = true;
93                 }
94                 else if( ( cntStr.equalsIgnoreCase( "gc" ) ) )
95                 {
96                     System.gc();
97                 }
98                 else
99                 {
100                     try
101                     {
102                         int concurrent = 100;
103                         CyclicBarrier barrier = new CyclicBarrier( concurrent );
104                         int cnt = Integer.parseInt( cntStr );
105                         int average = Math.max( cnt / concurrent, 1 );
106
107                         while( cnt > 0 )
108                         {
109                             Thread JavaDoc t = new Thread JavaDoc( new ActionRunner( instrumentable,
110                                                                      Math.min( average, cnt ),
111                                                                      barrier ) );
112                             t.start();
113
114                             if( cnt > 0 )
115                             {
116                                 cnt -= average;
117                             }
118
119                             if( cnt < 0 )
120                             {
121                                 cnt = 0;
122                             }
123                         }
124                     }
125                     catch( NumberFormatException JavaDoc e )
126                     {
127                     }
128                 }
129             }
130         }
131         finally
132         {
133             // Release the component
134
serviceManager.release( instrumentable );
135             instrumentable = null;
136
137             // Dispose of the ComponentManagerCreator. It will dispose all
138
// of its own components, including the ComponentManager
139
m_componentManagerCreator.dispose();
140         }
141
142         System.out.println();
143         System.out.println( "Exiting..." );
144         System.exit( 0 );
145     }
146
147     private static final class ActionRunner implements Runnable JavaDoc
148     {
149         private final int m_numIterations;
150         private final ExampleInstrumentable m_instrumentable;
151         private final CyclicBarrier m_barrier;
152
153         protected ActionRunner( ExampleInstrumentable instrumentable, int numIterations, CyclicBarrier barrier )
154         {
155             m_numIterations = numIterations;
156             m_instrumentable = instrumentable;
157             m_barrier = barrier;
158         }
159
160         public void run()
161         {
162             for( int i = 0; i < m_numIterations; i++ )
163             {
164                 m_instrumentable.doAction();
165             }
166
167             try
168             {
169                 m_barrier.barrier();
170             }
171             catch( Exception JavaDoc e )
172             {
173             }
174         }
175     }
176 }
177
178
Popular Tags