Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gsoft/util/FileObfuscatorManager.java


1   /*************************************************************************
2   Copyright (C) 2003  Steve Gee
3   stevesgee@cox.net
4   
5   This program is free software; you can redistribute it and/or
6   modify it under the terms of the GNU General Public License
7   as published by the Free Software Foundation; either version 2
8   of the License, or (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 General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *************************************************************************/
19  
20  package gsoft.util;
21  
22  import java.io.*;
23  import java.util.*;
24  import gsoft.xervlet.TagReader;
25  
26  public abstract class FileObfuscatorManager extends SecurityObfuscator{
27  
28    public abstract String fetchPropertyFile();
29    public abstract String fetchPropertyRootTag();
30  
31    private FileInputStream fileInputStream = null;
32    private DataOutputStream outputStream = null;
33    private StringBuffer configFile;
34    private TagReader propertyReader = null;
35    private String inputFile,outputFile;
36  
37    public void executeObfuscator(){
38      try{
39        loadProperties(fetchPropertyFile());
40      }catch(Exception ex){
41        System.out.println("ERROR READING CONFIGURATION FILE: " + fetchPropertyFile());
42        System.exit(0);
43      }
44    }
45  
46    public void executeObfuscator(String args[]){
47      try {
48        int stop = args.length;
49        int counter = 0;
50        boolean failed = true;
51        String error = "";
52        if(stop > 0){
53          while(counter < stop){
54            if (args[counter].equalsIgnoreCase("-d")) {
55              loadProperties(fetchPropertyFile());
56              decryptFile();
57              failed = false;
58            }
59            else if (args[counter].equalsIgnoreCase("-e")) {
60              loadProperties(fetchPropertyFile());
61              encryptFile();
62              failed = false;
63            }else if (args[counter].equalsIgnoreCase("-v")) {
64              loadProperties(fetchPropertyFile());
65              viewFile();
66              failed = false;
67            }else if (args[counter].equalsIgnoreCase("-h")) {
68              showSyntax();
69              System.exit(0);
70            }else if (args[counter].equalsIgnoreCase("-x")) {
71              showConfigExample();
72              System.exit(0);
73            }//end if-else
74            counter++;
75          }//end while
76        }//end if
77        if(failed){
78          showSyntax();
79          System.exit(0);
80        }
81  
82      }catch (Exception ex) {
83  //      ex.printStackTrace();
84      }
85    }
86  
87    private void loadProperties(String file) throws Exception{
88  System.out.println("Loading property file: " + file);
89      propertyReader = new TagReader();
90      propertyReader.setRootTag(fetchPropertyRootTag());
91      openFile(file);
92      propertyReader.parse(configFile.toString());
93      configFile.setLength(0);
94      setHashMap(propertyReader.getTagValue("CONFIG", "HASHMAP"));
95      inputFile = propertyReader.getTagValue("CONFIG", "INPUTFILE");
96      outputFile = propertyReader.getTagValue("CONFIG", "OUTPUTFILE");
97    }
98  
99    private void showConfigExample(){
100     System.out.println("The default file name is: obfuscator.properties");
101     System.out.println("The properties file must be in the listed format");
102     System.out.println("<PROPERTIES>");
103     System.out.println("\t<CONFIG>");
104     System.out.println("\t\t<INPUTFILE>/usr/local/share/config/myApp.config.xml</INPUTFILE>");
105     System.out.println("\t\t<OUTPUTFILE>/usr/local/myAppHome/myApp.config.xml.enc</OUTPUTFILE>");
106     System.out.println("\t\t<HASHMAP>Your-Hash-Map-For-The-Obfusticator</HASHMAP>");
107     System.out.println("\t</CONFIG>");
108     System.out.println("</PROPERTIES>");
109   }
110 
111 
112   public String decryptForUse() throws Exception{
113     openFile(outputFile);
114     return reverseObfuscator(configFile.toString() );
115   }
116 
117   private void encryptFile() throws Exception{
118     try{
119       openFile(inputFile);
120       writeFile(outputFile, reverseObfuscator(configFile.toString()));
121       try {
122         File deleteConfigFile = new File(inputFile);
123         deleteConfigFile.delete();
124       }catch (Exception ex) {
125         System.out.println("ERROR REMOVING OLD FILE");
126       }
127     }catch(FileNotFoundException fnfe){}
128   }
129 
130   private void decryptFile() throws Exception{
131     openFile(outputFile);
132     writeFile(inputFile,reverseObfuscator(configFile.toString()));
133   }
134 
135   private void viewFile() throws Exception{
136     openFile(outputFile);
137     System.out.println(reverseObfuscator(configFile.toString() ) );
138   }
139 
140   public String getDecryptedFile() throws Exception{
141     openFile(outputFile);
142     return reverseObfuscator(configFile.toString());
143   }
144 
145   private void openFile(String inputFile) throws Exception{
146     configFile = new StringBuffer();
147     try {
148       File file = new File(inputFile);
149       if(file.exists()){
150         fileInputStream = new FileInputStream(file);
151         int availble = fileInputStream.available();
152         byte bites[] = new byte[availble];
153         fileInputStream.read(bites);
154         configFile.append(new String(bites));
155       }else{
156         throw new FileNotFoundException();
157       }
158     }catch (Exception ex) {
159       System.out.println("File Not Found: " + inputFile);
160       throw ex;
161     }finally {
162       try {fileInputStream.close();}catch (Exception ex) {}finally { }
163     }
164   }
165 
166   private void writeFile(String outputFile, String dataOutput) throws Exception{
167     FileOutputStream foStream = null;
168     try {
169         outputStream = new DataOutputStream(new FileOutputStream(outputFile,false));
170         outputStream.writeBytes(dataOutput);
171     }catch (Exception ex) {
172       ex.printStackTrace();
173     }finally {
174       try {outputStream.close();}catch (Exception ex) {}finally { }
175       try {foStream.close();}catch (Exception ex) {}finally { }
176     }
177   }
178 
179   public void showSyntax(){
180     System.out.println("Invalid parameters or syntax");
181     System.out.println("\tjava -jar configObfuscator.jar -d | -e | -v | -h");
182     System.out.println("\t-d\tDecrypt Input File  Will decrypt the file vcm_par.config.xml.enc to cm_par.config.xml");
183     System.out.println("\t-e\tEncrypt Config File Will encrypt the file vcm_par.config.xml to vcm_par.config.xml.enc");
184     System.out.println("\t-v\tView the Encrypted Config File (no output file will be generated)");
185     System.out.println("\t-h\tThis screen");
186     System.out.println("\t-x\tobfuscator.properties config example");
187   }
188 
189 }