BrightSide Workbench Full Report + Source Code
WorkingDirectory.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.wd.files;
19 
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.text.ParseException;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.jdom.Document;
28 import org.jdom.Element;
29 import org.jdom.JDOMException;
30 import org.jdom.input.SAXBuilder;
31 import org.jdom.output.Format;
32 import org.jdom.output.XMLOutputter;
33 
38 public class WorkingDirectory extends Directory {
39 
40  public static final String
41  WD_ROOT = "/BBWorkDir",
42  WD_CONF_ROOT = "/.BBWDConf",
43  WD_CACHE = "/.BBWorkDir.cache";
44 
45  private String bbUser, bbHost, bbPort, bbContext;
46  private File wdHome, wdInternal, wdCache;
47 
48  public WorkingDirectory(String bbUser, String bbHost, String bbPort, String bbContext) {
49  this.bbUser = bbUser;
50  this.bbHost = bbHost;
51  this.bbPort = bbPort;
52  this.bbContext = bbContext;
53  }
54 
55  public String getBbContext() {
56  return bbContext == null ? "" : bbContext;
57  }
58 
59  public String getBbHost() {
60  return bbHost;
61  }
62 
63  public String getBbPort() {
64  return bbPort;
65  }
66 
67  public String getBbUser() {
68  return bbUser;
69  }
70 
71  public void initialize(String bbCreateDir, String bbCreatePath) throws ParseException {
72  wdHome = new File(System.getProperty("user.home") + WD_ROOT + "/" + bbHost);
73  if(!wdHome.exists()) {
74  wdHome.mkdirs();
75  }
76  wdInternal = new File(wdHome.getAbsolutePath() + WD_CONF_ROOT);
77  if(!wdInternal.exists()) {
78  wdInternal.mkdirs();
79  }
80  wdCache = new File(wdInternal.getAbsolutePath() + WD_CACHE);
81  initializeCache();
82  checkExisting();
84  if(bbCreateDir != null) {
85  Directory d = getDirs().getDir(bbCreateDir);
86  if(d == null) {
87  d = addDirectory(bbCreateDir);
88  d.setServerPath(bbCreatePath);
89  d.getSystemFile().mkdirs();
90  }
91  }
93  saveCache();
94  countFiles();
95  }
96 
97  public void reload() throws ParseException {
98  getDirs().clear();
99  getFiles().clear();
100  initializeCache();
101  checkExisting();
102  loadSystemFiles();
103  loadServerFiles();
104  saveCache();
105  countFiles();
106  }
107 
108  @Override
109  public String getSystemName() {
110  return wdHome.getAbsolutePath();
111  }
112 
113  private void initializeCache() throws ParseException {
114  SAXBuilder builder = new SAXBuilder();
115  Document doc;
116  try {
117  if(wdCache.exists()) {
118  doc = builder.build(wdCache);
119  readXML(doc.getRootElement());
120  }
121  } catch (IOException ex) {
122  Logger.getLogger(WorkingDirectory.class.getName()).log(Level.SEVERE, null, ex);
123  } catch (JDOMException ex) {
124  Logger.getLogger(WorkingDirectory.class.getName()).log(Level.SEVERE, null, ex);
125  }
126  }
127 
128  public void saveCache() {
129  Document doc = new Document(new Element("bb-working-directory"));
130  writeXML(doc.getRootElement());
131  try {
132  OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(wdCache), "UTF-8");
133  Format fm = Format.getPrettyFormat();
134  fm.setEncoding("UTF-8");
135  XMLOutputter xo = new XMLOutputter(fm);
136  xo.output(doc, fw);
137  fw.close();
138  } catch (IOException ex) {
139  Logger.getLogger(WorkingDirectory.class.getName()).log(Level.SEVERE, null, ex);
140  }
141  }
142 
143 }
Directory getDir(String dir)
void writeXML(Element root)
Definition: Directory.java:257
Directory addDirectory(String name)
Definition: Directory.java:193
void readXML(Element root)
Definition: Directory.java:239
void setServerPath(String serverPath)
Definition: Directory.java:103
void initialize(String bbCreateDir, String bbCreatePath)
WorkingDirectory(String bbUser, String bbHost, String bbPort, String bbContext)