1 package org.opensync.engine.server.protocol;
2
3 import java.io.File;
4 import java.io.FilenameFilter;
5 import java.io.IOException;
6 import java.net.URL;
7
8 import org.opensync.engine.server.Connector;
9 import org.opensync.engine.server.DefaultProtocol;
10 import org.opensync.engine.server.Protocol;
11 import org.opensync.engine.server.OpenSync;
12 import org.opensync.engine.server.OpenSyncException;
13 import org.opensync.engine.util.FileHelper;
14
15
16 /***
17 * The protocol used to get information from a win file server
18 *
19 * @version 1.0
20 * @author SOFTMED
21 */
22
23 public class WinFileProtocol extends DefaultProtocol{
24
25 /***
26 * The file extention when file treated
27 *
28 */
29 protected final static String EXT_DONE = ".done";
30
31 private FileHelper aFileHelper=null;
32 private long readblockSize = 1000;
33
34 public void setReadblockSize(long size) {
35 this.readblockSize = size;
36 }
37 public void setAFileHelper(FileHelper afh) {
38 this.aFileHelper = afh;
39 }
40
41
42 public FileHelper getAFileHelper() {
43 if (this.aFileHelper == null) this.aFileHelper = new FileHelper();
44 return this.aFileHelper;
45 }
46 /***
47 * Construct a WinFileProtocl
48 *
49 */
50 public WinFileProtocol() {
51 setName(Protocol.WIN_FILE);
52 aFileHelper = new FileHelper();
53 }
54
55 public void init() {
56 this.getAFileHelper().setStarting(true);
57 }
58 /***
59 * Put the <code>fileContent</code> in the file <code>fileName</code> int
60 * the <code>folder</code> directory
61 *
62 * @param fileContent the file content
63 * @param fileName the file name
64 * @exception OpenSyncException
65 * @exception IOException
66 */
67 public void putView(String fileContent,String fileName)throws OpenSyncException,IOException{
68 String fileSeparator = System.getProperty("file.separator");
69 String path;
70 Connector connector = getConnector();
71
72 if(!connector.getUrl().equals("")){
73 path = new URL(connector.getUrl() + fileSeparator + folder).toExternalForm();
74 }
75 else{
76 path = folder;
77 }
78 File file = new File(path);
79 String configFolder = System.getProperty("opensync.configfolder");
80 configFolder = (configFolder == null ? "" : configFolder);
81
82 if (!(file.exists() && file.isDirectory())){
83 path = OpenSync.getInstance().getDirectoryPath("etc"+fileSeparator+configFolder+fileSeparator+path, false);
84 file = new File(path);
85 if(!(file.exists() && file.isDirectory())){
86 throw new OpenSyncException("error.directory.not-found",new Object[]{path});
87 }
88 }
89 if(fileName.indexOf("*") == -1){
90 this.getAFileHelper().stringAppendToFile(fileContent,path + fileSeparator + fileName);
91 }
92 else{
93 final String pattern = fileName.substring(0,fileName.indexOf("*"));
94 String[]files = file.list(
95 new FilenameFilter(){
96 public boolean accept(File dir, String name){
97 return name.startsWith(pattern);
98 }
99 }
100 );
101 FileHelper.stringToFile(fileContent,path + fileSeparator + pattern + "." + files.length);
102 }
103 }
104 /***
105 * Get the content of files <code>fileName</code>
106 *
107 * @param fileName the file name (may use *)
108 * @exception OpenSyncException
109 * @exception IOException
110 */
111 public String[] getViews(String fileName)throws OpenSyncException,IOException{
112
113 if (fileName == null) throw new OpenSyncException("error.file.not-found", new Object[]{"null"});
114 String fileSeparator = System.getProperty("file.separator");
115 String path;
116 Connector connector = getConnector();
117
118 if(!connector.getUrl().equals("")){
119 path = new URL(connector.getUrl() + fileSeparator + folder).toExternalForm();
120 }
121 else{
122 path = folder;
123 }
124
125 String configFolder = System.getProperty("opensync.configfolder");
126 configFolder = (configFolder == null ? "" : configFolder);
127
128 File file = new File(path);
129 if (!(file.exists() && file.isDirectory())){
130 path = OpenSync.getInstance().getDirectoryPath("etc"+fileSeparator+configFolder+fileSeparator+path, true);
131 file = new File(path);
132 if(!(file.exists() && file.isDirectory())){
133 throw new OpenSyncException("error.directory.not-found",new Object[]{path});
134 }
135 }
136 if (fileName.indexOf("*") == -1){
137 return new String[]{this.getAFileHelper().fileToString(path + fileSeparator + fileName, readblockSize)};
138 }
139 else{
140 final String pattern = fileName.substring(0,fileName.indexOf("*"));
141 String[]files = file.list(
142 new FilenameFilter(){
143 public boolean accept(File dir, String name){
144 return name.startsWith(pattern) && !name.endsWith(EXT_DONE);
145 }
146 }
147 );
148 String[] views = new String[files.length];
149 for(int i=0;i<files.length;i++){
150 views[i] = FileHelper.fileToString(path + fileSeparator + files[i]);
151 }
152 return views;
153 }
154 }
155
156 /***
157 * Clean files (rename or remove) if necessary after the synchronisation done.
158 *
159 * @param fileName the file name to clean
160 * @exception OpenSyncException
161 * @exception IOException
162 */
163 public void clean(String fileName)throws OpenSyncException,IOException{
164 String fileSeparator = System.getProperty("file.separator");
165 String path;
166
167 try {
168 Connector connector = getConnector();
169 if(!connector.getUrl().equals("")){
170 path = new URL(connector.getUrl() + fileSeparator + folder).toExternalForm();
171 }
172 else{
173 path = folder;
174 }
175
176 String configFolder = System.getProperty("opensync.configfolder");
177 configFolder = (configFolder == null ? "" : configFolder);
178
179 File file = new File(path);
180 if (!(file.exists() && file.isDirectory())){
181 path = OpenSync.getInstance().getDirectoryPath("etc"+fileSeparator+configFolder+fileSeparator+path, true);
182 file = new File(path);
183 if(!(file.exists() && file.isDirectory())){
184 throw new OpenSyncException("error.directory.not-found",new Object[]{path});
185 }
186 }
187
188 if(fileName.indexOf("*") == -1){
189 if(isRemoveFile()){
190 new File(path + fileSeparator + fileName).delete();
191 }
192 else if(isRenameFile()){
193 new File(path + fileSeparator + fileName).renameTo(
194 new File(path + fileSeparator + fileName + EXT_DONE)
195 );
196 }
197 }
198 else{
199 final String pattern = fileName.substring(0,fileName.indexOf("*"));
200 String[]files = file.list(
201 new FilenameFilter(){
202 public boolean accept(File dir, String name){
203 return name.startsWith(pattern) && !name.endsWith(EXT_DONE);
204 }
205 }
206 );
207 for(int i=0;i<files.length;i++){
208 if(isRemoveFile()){
209 new File(path + fileSeparator + files[i]).delete();
210 }
211 else if(isRenameFile()){
212 new File(path + fileSeparator + files[i]).renameTo(
213 new File(path + fileSeparator + files[i] + EXT_DONE)
214 );
215 }
216 }
217 }
218 } catch (OpenSyncException ssex) {
219 throw ssex;
220 } catch (IOException ioex) {
221 throw ioex;
222 }
223 finally {
224 if (this.aFileHelper != null) {
225 this.aFileHelper.clean();
226 this.aFileHelper = null;
227 }
228 }
229 }
230
231
232
233 }
234
235
236
237