KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.excalibur.instrument.manager.http.server.HTTPRedirect;
27 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
28 import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor;
29 import org.apache.excalibur.instrument.manager.NoSuchInstrumentSampleException;
30
31 /**
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $Revision: 1.6 $ $Date: 2004/03/06 14:01:28 $
35  * @since 4.1
36  */

37 public class HTMLSampleLeaseHandler
38     extends AbstractHTMLHandler
39 {
40     /*---------------------------------------------------------------
41      * Constructors
42      *-------------------------------------------------------------*/

43     /**
44      * Creates a new HTMLSampleLeaseHandler.
45      *
46      * @param manager Reference to the DefaultInstrumentManager.
47      * @param connector The InstrumentManagerHTTPConnector.
48      */

49     public HTMLSampleLeaseHandler( DefaultInstrumentManager manager,
50                                    InstrumentManagerHTTPConnector connector )
51     {
52         super( "/sample-lease.html", manager, connector );
53     }
54     
55     /*---------------------------------------------------------------
56      * AbstractHTTPURLHandler Methods
57      *-------------------------------------------------------------*/

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

65     public void doGet( String JavaDoc path, Map JavaDoc parameters, PrintWriter JavaDoc out )
66         throws IOException JavaDoc
67     {
68         String JavaDoc name = getParameter( parameters, "name" );
69         long lease = getLongParameter( parameters, "lease" );
70         String JavaDoc instrument = getParameter( parameters, "instrument", null );
71         String JavaDoc chart = getParameter( parameters, "chart", null );
72         
73         InstrumentSampleDescriptor desc;
74         try
75         {
76             desc = getInstrumentManager().locateInstrumentSampleDescriptor( name );
77         }
78         catch ( NoSuchInstrumentSampleException e )
79         {
80             // Sample no longer exists, go back to the parent instrument.
81
int pos = name.lastIndexOf( '.' );
82             if ( pos >= 0 )
83             {
84                 throw new HTTPRedirect(
85                     "instrument.html?name=" + urlEncode( name.substring( 0, pos ) ) );
86             }
87             else
88             {
89                 throw new HTTPRedirect( "instrument-manager.html" );
90             }
91         }
92         
93         // The instrument manager will do its own tests of the lease, but the
94
// restrictions on this connector may be stronger so they must be tested
95
// here as well.
96
lease = Math.max( 1, Math.min( lease, getConnector().getMaxLeasedSampleLease() ) );
97         
98         if ( getInstrumentManager().getLeaseSampleCount() >= getConnector().getMaxLeasedSamples() )
99         {
100             lease = 1;
101         }
102         
103         // Renew the lease
104
desc.extendLease( lease );
105         
106         if ( instrument != null )
107         {
108             // Go to the instrument page.
109
int pos = name.lastIndexOf( '.' );
110             if ( pos >= 0 )
111             {
112                 throw new HTTPRedirect(
113                     "instrument.html?name=" + urlEncode( name.substring( 0, pos ) ) );
114             }
115             else
116             {
117                 throw new HTTPRedirect( "instrumentable.html" );
118             }
119         }
120         else
121         {
122             // Redirect to the sample page.
123
throw new HTTPRedirect( "sample.html?name=" + urlEncode( desc.getName() )
124                 + ( chart == null ? "" : "&chart=true" ) );
125         }
126         
127     }
128             
129     /*---------------------------------------------------------------
130      * Methods
131      *-------------------------------------------------------------*/

132 }
133
134
Popular Tags