KickJava   Java API By Example, From Geeks To Geeks.

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


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 HTMLCreateSampleHandler
40     extends AbstractHTMLHandler
41 {
42     /*---------------------------------------------------------------
43      * Constructors
44      *-------------------------------------------------------------*/

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

51     public HTMLCreateSampleHandler( DefaultInstrumentManager manager,
52                                     InstrumentManagerHTTPConnector connector )
53     {
54         super( "/create-sample.html", 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         
77         InstrumentDescriptor desc;
78         try
79         {
80             desc = getInstrumentManager().locateInstrumentDescriptor( name );
81         }
82         catch ( NoSuchInstrumentException e )
83         {
84             // Sample no longer exists, go back to the parent instrument.
85
int pos = name.lastIndexOf( '.' );
86             if ( pos >= 0 )
87             {
88                 throw new HTTPRedirect(
89                     "instrumentable.html?name=" + urlEncode( name.substring( 0, pos ) ) );
90             }
91             else
92             {
93                 throw new HTTPRedirect( "instrument-manager.html" );
94             }
95         }
96         
97         // The instrument manager will do its own tests of the lease, but the
98
// restrictions on this connector may be stronger so they must be tested
99
// here as well.
100
size = Math.max( 1, Math.min( size, getConnector().getMaxLeasedSampleSize() ) );
101         lease = Math.max( 1, Math.min( lease, getConnector().getMaxLeasedSampleLease() ) );
102         
103         if ( getInstrumentManager().getLeaseSampleCount() >= getConnector().getMaxLeasedSamples() )
104         {
105             lease = 1;
106         }
107         
108         // Register the new lease
109
InstrumentSampleDescriptor sample;
110         try
111         {
112             sample = desc.createInstrumentSample( description, interval, size, lease, type );
113         }
114         catch ( IllegalArgumentException JavaDoc e )
115         {
116             // The sample type is not valid.
117
throw new FileNotFoundException JavaDoc( e.getMessage() );
118         }
119         catch ( IllegalStateException JavaDoc e )
120         {
121             // The sample type was incompatible with the instrument.
122
throw new FileNotFoundException JavaDoc( e.getMessage() );
123         }
124         
125         // Redirect to the new sample page.
126
throw new HTTPRedirect( "sample.html?name=" + urlEncode( sample.getName() ) );
127     }
128             
129     /*---------------------------------------------------------------
130      * Methods
131      *-------------------------------------------------------------*/

132 }
133
134
Popular Tags