KickJava   Java API By Example, From Geeks To Geeks.

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


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.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  * @since 1.2
35  */

36 public class XMLSampleLeasesHandler
37     extends AbstractXMLHandler
38 {
39     /*---------------------------------------------------------------
40      * Constructors
41      *-------------------------------------------------------------*/

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

48     public XMLSampleLeasesHandler( DefaultInstrumentManager manager,
49                                    InstrumentManagerHTTPConnector connector )
50     {
51         super( "/sample-leases.xml", manager, connector );
52     }
53     
54     /*---------------------------------------------------------------
55      * AbstractHTTPURLHandler Methods
56      *-------------------------------------------------------------*/

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

64     public void doGet( String JavaDoc path, Map JavaDoc parameters, PrintWriter JavaDoc out )
65         throws IOException JavaDoc
66     {
67         String JavaDoc[] names = getParameters( parameters, "name" );
68         long[] leases = getLongParameters( parameters, "lease", 0 );
69         boolean packed = getBooleanParameter( parameters, "packed", false );
70         
71         if ( names.length != leases.length )
72         {
73             throw new FileNotFoundException JavaDoc(
74                 "The number of leases values not equal to the number of names." );
75         }
76         
77         out.println( InstrumentManagerHTTPConnector.XML_BANNER );
78         if ( names.length > 0 )
79         {
80             outputLine( out, "", packed, "<samples>" );
81             
82             for ( int i = 0; i < names.length; i++ )
83             {
84                 String JavaDoc name = names[i];
85                 long lease = leases[i];
86                 
87                 InstrumentSampleDescriptor desc;
88                 try
89                 {
90                     desc = getInstrumentManager().locateInstrumentSampleDescriptor( name );
91                 }
92                 catch ( NoSuchInstrumentSampleException e )
93                 {
94                     // Not found, ignore.
95
desc = null;
96                 }
97                 
98                 if ( desc != null )
99                 {
100                     // The instrument manager will do its own tests of the lease, but the
101
// restrictions on this connector may be stronger so they must be tested
102
// here as well.
103
lease = Math.max(
104                         1, Math.min( lease, getConnector().getMaxLeasedSampleLease() ) );
105                     
106                     if ( getInstrumentManager().getLeaseSampleCount() >=
107                         getConnector().getMaxLeasedSamples() )
108                     {
109                         lease = 1;
110                     }
111                     
112                     // Renew the lease
113
desc.extendLease( lease );
114                     
115                     outputSample( out, desc, " ", packed );
116                 }
117             }
118             
119             outputLine( out, "", packed, "</samples>" );
120         }
121         else
122         {
123             outputLine( out, "", packed, "<samples/>" );
124         }
125     }
126     
127     /*---------------------------------------------------------------
128      * Methods
129      *-------------------------------------------------------------*/

130 }
131
132
Popular Tags