KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > manager > http > XMLCreateSamplesHandler


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
20 package org.apache.excalibur.instrument.manager.http;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.excalibur.instrument.manager.http.server.HTTPRedirect;
28 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
29 import org.apache.excalibur.instrument.manager.InstrumentDescriptor;
30 import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor;
31 import org.apache.excalibur.instrument.manager.NoSuchInstrumentException;
32
33 /**
34  * Handler which can be used to create multiple samples at once.
35  *
36  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
37  * @since 1.2
38  */

39 public class XMLCreateSamplesHandler
40     extends AbstractXMLHandler
41 {
42     /*---------------------------------------------------------------
43      * Constructors
44      *-------------------------------------------------------------*/

45     /**
46      * Creates a new XMLCreateSamplesHandler.
47      *
48      * @param manager Reference to the DefaultInstrumentManager.
49      * @param connector The InstrumentManagerHTTPConnector.
50      */

51     public XMLCreateSamplesHandler( DefaultInstrumentManager manager,
52                                     InstrumentManagerHTTPConnector connector )
53     {
54         super( "/create-samples.xml", manager, connector );
55     }
56     
57     /*---------------------------------------------------------------
58      * AbstractHTTPURLHandler Methods
59      *-------------------------------------------------------------*/

60     /**
61      * Handles the specified request.
62      *
63      * @param The full path being handled.
64      * @param parameters A Map of the parameters in the request.
65      * @param os The PrintWriter to write the result to.
66      */

67     public void doGet( String JavaDoc path, Map JavaDoc parameters, PrintWriter JavaDoc out )
68         throws IOException JavaDoc
69     {
70         String JavaDoc[] names = getParameters( parameters, "name" );
71         String JavaDoc[] descriptions = getParameters( parameters, "description" );
72         long[] intervals = getLongParameters( parameters, "interval", 0 );
73         int[] sizes = getIntegerParameters( parameters, "size", 0 );
74         long[] leases = getLongParameters( parameters, "lease", 0 );
75         int[] types = getIntegerParameters( parameters, "type", 0 );
76         boolean packed = getBooleanParameter( parameters, "packed", false );
77         
78         if ( names.length != descriptions.length )
79         {
80             throw new FileNotFoundException JavaDoc(
81                 "The number of descriptions not equal to the number of names." );
82         }
83         if ( names.length != intervals.length )
84         {
85             throw new FileNotFoundException JavaDoc(
86                 "The number of intervals not equal to the number of names." );
87         }
88         if ( names.length != sizes.length )
89         {
90             throw new FileNotFoundException JavaDoc(
91                 "The number of sizes not equal to the number of names." );
92         }
93         if ( names.length != leases.length )
94         {
95             throw new FileNotFoundException JavaDoc(
96                 "The number of leases not equal to the number of names." );
97         }
98         if ( names.length != types.length )
99         {
100             throw new FileNotFoundException JavaDoc(
101                 "The number of types not equal to the number of names." );
102         }
103         
104         out.println( InstrumentManagerHTTPConnector.XML_BANNER );
105         if ( names.length > 0 )
106         {
107             outputLine( out, "", packed, "<samples>" );
108             
109             for ( int i = 0; i < names.length; i++ )
110             {
111                 String JavaDoc name = names[i];
112                 String JavaDoc description = descriptions[i];
113                 long interval = intervals[i];
114                 int size = sizes[i];
115                 long lease = leases[i];
116                 int type = types[i];
117                 
118                 InstrumentDescriptor desc;
119                 try
120                 {
121                     desc = getInstrumentManager().locateInstrumentDescriptor( name );
122                 }
123                 catch ( NoSuchInstrumentException e )
124                 {
125                     // Not found, ignore.
126
desc = null;
127                 }
128                 
129                 if ( desc != null )
130                 {
131                     // The instrument manager will do its own tests of the lease, but the
132
// restrictions on this connector may be stronger so they must be tested
133
// here as well.
134
size = Math.max( 1, Math.min( size, getConnector().getMaxLeasedSampleSize() ) );
135                     lease = Math.max(
136                         1, Math.min( lease, getConnector().getMaxLeasedSampleLease() ) );
137                     
138                     if ( getInstrumentManager().getLeaseSampleCount()
139                         >= getConnector().getMaxLeasedSamples() )
140                     {
141                         lease = 1;
142                     }
143                     
144                     // Register the new lease
145
InstrumentSampleDescriptor sample;
146                     try
147                     {
148                         sample =
149                             desc.createInstrumentSample( description, interval, size, lease, type );
150                     }
151                     catch ( IllegalArgumentException JavaDoc e )
152                     {
153                         // The sample type is not valid.
154
throw new FileNotFoundException JavaDoc( e.getMessage() );
155                     }
156                     catch ( IllegalStateException JavaDoc e )
157                     {
158                         // The sample type was incompatible with the instrument.
159
throw new FileNotFoundException JavaDoc( e.getMessage() );
160                     }
161                     
162                     outputSample( out, sample, " ", packed );
163                 }
164             }
165             
166             outputLine( out, "", packed, "</samples>" );
167         }
168         else
169         {
170             outputLine( out, "", packed, "<samples/>" );
171         }
172     }
173             
174     /*---------------------------------------------------------------
175      * Methods
176      *-------------------------------------------------------------*/

177 }
178
179
Popular Tags