KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailrepository > filepair > File_Persistent_Stream_Repository


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * 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. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.mailrepository.filepair;
19
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import org.apache.avalon.cornerstone.services.store.StreamRepository;
27 import org.apache.avalon.excalibur.io.IOUtil;
28
29 /**
30  * Implementation of a StreamRepository to a File.
31  * TODO: -retieve(String key) should return a FilterInputStream to allow
32  * mark and reset methods. (working not like BufferedInputStream!!!)
33  *
34  */

35 public class File_Persistent_Stream_Repository
36     extends AbstractFileRepository
37     implements StreamRepository
38 {
39     protected final HashMap JavaDoc m_inputs = new HashMap JavaDoc();
40     protected final HashMap JavaDoc m_outputs = new HashMap JavaDoc();
41
42     protected String JavaDoc getExtensionDecorator()
43     {
44         return ".FileStreamStore";
45     }
46
47     /**
48      * Get the object associated to the given unique key.
49      */

50     public synchronized InputStream JavaDoc get( final String JavaDoc key )
51     {
52         try
53         {
54             final ResettableFileInputStream stream =
55                 new ResettableFileInputStream( getFile( key ) );
56
57             final Object JavaDoc o = m_inputs.get( key );
58             if( null == o )
59             {
60                 m_inputs.put( key, stream );
61             }
62             else if( o instanceof ArrayList JavaDoc )
63             {
64                 ( (ArrayList JavaDoc)o ).add( stream );
65             }
66             else
67             {
68                 final ArrayList JavaDoc list = new ArrayList JavaDoc();
69                 list.add( o );
70                 list.add( stream );
71                 m_inputs.put( key, list );
72             }
73
74             return stream;
75         }
76         catch( final IOException JavaDoc ioe )
77         {
78             final String JavaDoc message = "Exception caught while retrieving a stream ";
79             getLogger().warn( message, ioe );
80             throw new RuntimeException JavaDoc( message + ": " + ioe );
81         }
82     }
83
84     /**
85      * Store the given object and associates it to the given key
86      */

87     public synchronized OutputStream JavaDoc put( final String JavaDoc key )
88     {
89         try
90         {
91             final OutputStream JavaDoc outputStream = getOutputStream( key );
92             final BufferedOutputStream JavaDoc stream = new BufferedOutputStream JavaDoc( outputStream );
93
94             final Object JavaDoc o = m_outputs.get( key );
95             if( null == o )
96             {
97                 m_outputs.put( key, stream );
98             }
99             else if( o instanceof ArrayList JavaDoc )
100             {
101                 ( (ArrayList JavaDoc)o ).add( stream );
102             }
103             else
104             {
105                 final ArrayList JavaDoc list = new ArrayList JavaDoc();
106                 list.add( o );
107                 list.add( stream );
108                 m_outputs.put( key, list );
109             }
110
111             return stream;
112         }
113         catch( final IOException JavaDoc ioe )
114         {
115             final String JavaDoc message = "Exception caught while storing a stream ";
116             getLogger().warn( message, ioe );
117             throw new RuntimeException JavaDoc( message + ": " + ioe );
118         }
119     }
120
121     public synchronized void remove( final String JavaDoc key )
122     {
123         Object JavaDoc o = m_inputs.remove( key );
124         if( null != o )
125         {
126             if( o instanceof InputStream JavaDoc )
127             {
128                 IOUtil.shutdownStream( (InputStream JavaDoc)o );
129             }
130             else
131             {
132                 final ArrayList JavaDoc list = (ArrayList JavaDoc)o;
133                 final int size = list.size();
134
135                 for( int i = 0; i < size; i++ )
136                 {
137                     IOUtil.shutdownStream( (InputStream JavaDoc)list.get( i ) );
138                 }
139             }
140         }
141
142         o = m_outputs.remove( key );
143         if( null != o )
144         {
145             if( o instanceof OutputStream JavaDoc )
146             {
147                 IOUtil.shutdownStream( (OutputStream JavaDoc)o );
148             }
149             else
150             {
151                 final ArrayList JavaDoc list = (ArrayList JavaDoc)o;
152                 final int size = list.size();
153
154                 for( int i = 0; i < size; i++ )
155                 {
156                     IOUtil.shutdownStream( (OutputStream JavaDoc)list.get( 0 ) );
157                 }
158             }
159         }
160
161         super.remove( key );
162     }
163
164     public long getSize(final String JavaDoc key) {
165         try {
166             return getFile(key).length();
167         }
168         catch(IOException JavaDoc e) {
169             return 0;
170         }
171     }
172 }
173
Popular Tags