KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > descriptor > softpkg > ccm > LinkDeployer


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Briclet Frédéric.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.openccm.descriptor.softpkg.ccm;
27
28
29 /**
30  * The link deployer provide an URL on a ressource
31  *
32  * @author <a HREF="mailto:briclet@lifl.fr">Briclet Frédéric</a>
33  *
34  * @version 0.1
35  */

36 public class LinkDeployer
37        extends LinkDeployerContext
38     {
39     // ==================================================================
40
//
41
// Internal state.
42
//
43
// ==================================================================
44
//temporary file to store the remote ressource
45
private java.io.File JavaDoc tmpFile;
46     // ==================================================================
47
//
48
// Constructors.
49
//
50
// ==================================================================
51

52     // ==================================================================
53
//
54
// Internal methods.
55
//
56
// ==================================================================
57
private String JavaDoc
58     getValeur()
59     {
60         if(getLink().getHref()!=null)
61             return getLink().getHref().trim();
62         else return getLink().getValue().trim();
63     }
64     
65     private java.io.InputStream JavaDoc
66     getInputStreamInternal()
67     throws java.io.IOException JavaDoc
68     {
69
70         
71         java.net.URL JavaDoc url=new java.net.URL JavaDoc(getValeur());
72         
73         //The required file is a local file we don't copy it
74
if(getValeur().startsWith("file:"))
75             return url.openStream();
76
77
78        //file is already prensent in the deployer
79
if(tmpFile!=null)
80         return new java.io.FileInputStream JavaDoc(tmpFile);
81
82        //File was already downloaded by an another deployer
83
if(getRootDeployerContext().isAlreadyDownloaded(getValeur()))
84        {
85             tmpFile=getRootDeployerContext().getTemporaryFile(getValeur());
86             return new java.io.FileInputStream JavaDoc(tmpFile);
87        }
88             
89         //file was not yet downloaded
90
tmpFile = java.io.File.createTempFile(
91              /* the url.getFile() value contains too much special characters
92                  like ~, ?, #, etc.
93                  so we change
94                      url.getFile()
95                  into an explicit filename construction
96                      OpenCCM_<the file without its location>_<time in ms>
97                  */

98                  "OpenCCM_tmp_"
99                  +url.getPath().substring(
100                      url.getPath().lastIndexOf('/')+1)
101                  +"_"
102               ,System.currentTimeMillis()+"");
103
104         java.io.FileOutputStream JavaDoc fos=new java.io.FileOutputStream JavaDoc(tmpFile);
105         java.io.InputStream JavaDoc is=url.openStream();
106         byte [] buff=new byte[32000];
107         int readed=is.read(buff,0,32000);
108         while(readed>0)
109         {
110              fos.write(buff,0,readed);
111              readed=is.read(buff,0,32000);
112         }
113         fos.close();
114         getRootDeployerContext().connectTemporaryFile(tmpFile,getValeur());
115         return new java.io.FileInputStream JavaDoc(tmpFile);
116     }
117     // ==================================================================
118
//
119
// Public methods.
120
//
121
// ==================================================================
122
/**
123      * Return the stringified URL to the ressource.
124      */

125     public String JavaDoc
126     getReference()
127     {
128         return getValeur();
129     }
130
131
132
133     public java.io.InputStream JavaDoc
134     getInputStream()
135     throws java.io.IOException JavaDoc
136     {
137         java.io.InputStream JavaDoc is=getInputStreamInternal();
138         getRootDeployerContext().connectOpenInputStream(is);
139         return is;
140     }
141
142 }
143
Popular Tags