KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  * @version CVS $Revision: 1.6 $ $Date: 2004/03/06 14:01:28 $
37  * @since 4.1
38  */

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

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

51     public XMLCreateSampleHandler( DefaultInstrumentManager manager,
52                                    InstrumentManagerHTTPConnector connector )
53     {
54         super( "/create-sample.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 name = getParameter( parameters, "name" );
71         String JavaDoc description = getParameter( parameters, "description" );
72         long interval = getLongParameter( parameters, "interval" );
73         int size = getIntegerParameter( parameters, "size" );
74         long lease = getLongParameter( parameters, "lease" );
75         int type = getIntegerParameter( parameters, "type" );
76         boolean packed = getBooleanParameter( parameters, "packed", false );
77         
78         InstrumentDescriptor desc;
79         try
80         {
81             desc = getInstrumentManager().locateInstrumentDescriptor( name );
82         }
83         catch ( NoSuchInstrumentException e )
84         {
85             throw new FileNotFoundException JavaDoc(
86                 "The specified instrument does not exist: " + name );
87         }
88         
89         // The instrument manager will do its own tests of the lease, but the
90
// restrictions on this connector may be stronger so they must be tested
91
// here as well.
92
size = Math.max( 1, Math.min( size, getConnector().getMaxLeasedSampleSize() ) );
93         lease = Math.max( 1, Math.min( lease, getConnector().getMaxLeasedSampleLease() ) );
94         
95         if ( getInstrumentManager().getLeaseSampleCount() >= getConnector().getMaxLeasedSamples() )
96         {
97             lease = 1;
98         }
99         
100         // Register the new lease
101
InstrumentSampleDescriptor sample;
102         try
103         {
104             sample = desc.createInstrumentSample( description, interval, size, lease, type );
105         }
106         catch ( IllegalArgumentException JavaDoc e )
107         {
108             // The sample type is not valid.
109
throw new FileNotFoundException JavaDoc( e.getMessage() );
110         }
111         catch ( IllegalStateException JavaDoc e )
112         {
113             // The sample type was incompatible with the instrument.
114
throw new FileNotFoundException JavaDoc( e.getMessage() );
115         }
116         
117         out.println( InstrumentManagerHTTPConnector.XML_BANNER );
118         outputSample( out, sample, "", packed );
119     }
120             
121     /*---------------------------------------------------------------
122      * Methods
123      *-------------------------------------------------------------*/

124 }
125
126
Popular Tags