KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > masterstore > AbstractFilePersistentStreamRepository


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 package org.apache.avalon.cornerstone.blocks.masterstore;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.BufferedOutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 /**
29  * @author Paul Hammant
30  * @version $Revision: 1.8 $
31  */

32 public abstract class AbstractFilePersistentStreamRepository extends AbstractFileRepository {
33
34     protected final HashMap JavaDoc m_inputs = new HashMap JavaDoc();
35     protected final HashMap JavaDoc m_outputs = new HashMap JavaDoc();
36
37     /**
38      * Get the object associated to the given unique key.
39      */

40     public synchronized InputStream JavaDoc get( final String JavaDoc key )
41     {
42         try
43         {
44             final ResettableFileInputStream stream =
45                 new ResettableFileInputStream( getFile( key ) );
46
47             final Object JavaDoc o = m_inputs.get( key );
48             if( null == o )
49             {
50                 m_inputs.put( key, stream );
51             }
52             else if( o instanceof ArrayList JavaDoc )
53             {
54                 ( (ArrayList JavaDoc)o ).add( stream );
55             }
56             else
57             {
58                 final ArrayList JavaDoc list = new ArrayList JavaDoc();
59                 list.add( o );
60                 list.add( stream );
61                 m_inputs.put( key, stream );
62             }
63
64             return stream;
65         }
66         catch( final IOException JavaDoc ioe )
67         {
68             final String JavaDoc message = "Exception caught while retrieving a stream ";
69             monitor.unExpectedIOException(File_Persistent_Stream_Repository.class, message, ioe);
70             throw new RuntimeException JavaDoc( message + ": " + ioe );
71         }
72     }
73
74     /**
75      * Store the given object and associates it to the given key
76      */

77     public synchronized OutputStream JavaDoc put( final String JavaDoc key )
78     {
79         try
80         {
81             final OutputStream JavaDoc outputStream = getOutputStream( key );
82             final BufferedOutputStream JavaDoc stream = new BufferedOutputStream JavaDoc( outputStream );
83
84             final Object JavaDoc o = m_outputs.get( key );
85             if( null == o )
86             {
87                 m_outputs.put( key, stream );
88             }
89             else if( o instanceof ArrayList JavaDoc )
90             {
91                 ( (ArrayList JavaDoc)o ).add( stream );
92             }
93             else
94             {
95                 final ArrayList JavaDoc list = new ArrayList JavaDoc();
96                 list.add( o );
97                 list.add( stream );
98                 m_outputs.put( key, stream );
99             }
100
101             return stream;
102         }
103         catch( final IOException JavaDoc ioe )
104         {
105             final String JavaDoc message = "Exception caught while storing a stream ";
106             monitor.unExpectedIOException(File_Persistent_Stream_Repository.class, message, ioe);
107             throw new RuntimeException JavaDoc( message + ": " + ioe );
108         }
109     }
110
111     public void remove( final String JavaDoc key )
112     {
113         Object JavaDoc o = m_inputs.remove( key );
114         if( null != o )
115         {
116             if( o instanceof InputStream JavaDoc )
117             {
118                 IOUtil.shutdownStream( (InputStream JavaDoc)o );
119             }
120             else
121             {
122                 final ArrayList JavaDoc list = (ArrayList JavaDoc)o;
123                 final int size = list.size();
124
125                 for( int i = 0; i < size; i++ )
126                 {
127                     IOUtil.shutdownStream( (InputStream JavaDoc)list.get( i ) );
128                 }
129             }
130         }
131
132         o = m_outputs.remove( key );
133         if( null != o )
134         {
135             if( o instanceof OutputStream JavaDoc )
136             {
137                 IOUtil.shutdownStream( (OutputStream JavaDoc)o );
138             }
139             else
140             {
141                 final ArrayList JavaDoc list = (ArrayList JavaDoc)o;
142                 final int size = list.size();
143
144                 for( int i = 0; i < size; i++ )
145                 {
146                     IOUtil.shutdownStream( (OutputStream JavaDoc)list.get( 0 ) );
147                 }
148             }
149         }
150
151         super.remove( key );
152     }
153
154     protected String JavaDoc getExtensionDecorator()
155     {
156         return ".FileStreamStore";
157     }
158 }
159
Popular Tags