Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.wintindustries.pfserver.PFDefaultLibrary.FileServices.PFFileServiceLocal; import com.wintindustries.pdffilter.FileManager.PFFileUtilities; import com.wintindustries.pdffilter.PFData.PFFileAcsessOptions; import com.wintindustries.pdffilter.PFData.PFFileService; import com.wintindustries.pdffilter.PFData.PFFileServiceConfig; import com.wintindustries.pdffilter.PFDocument.PFDocument; import com.wintindustries.pdffilter.PFDocument.PFFile; import com.wintindustries.pdffilter.PFDocument.PFGenericDocument.PFGenericDocument; import com.wintindustries.pdffilter.PFExceptions.PFData.PFFileServiceConnectionException; import com.wintindustries.pdffilter.PFExceptions.PFData.PFFileServiceConnectionExceptionAcsessDenied; import com.wintindustries.pdffilter.PFExceptions.PFData.PFFileServiceConnectionExceptionReadOnly; import com.wintindustries.pdffilter.PFLocation.PFFolder; import com.wintindustries.pdffilter.PFLocation.PFLocation; import com.wintindustries.pdffilter.PFLocation.testabstract; import com.wintindustries.pdffilter.PFLocation.testconcrete; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.FilePermission; import java.security.AccessController; import java.io.File; import java.io.FileFilter; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.FilenameFilter; import javax.xml.transform.dom.DOMSource; import org.apache.commons.io.FileUtils; /** * * @author Admin */ public class PFFileServiceLocal extends PFFileService { PFFileServiceLocalConfig config; testconcrete a = new testconcrete(); @Override public boolean writeDomSourceToFile(DOMSource source) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public synchronized boolean connect() throws PFFileServiceConnectionException { // check for read write acsess File f = new File(config.getRootDirectoryURL()); String path = config.getRootDirectoryURL() + "*"; f.mkdirs(); String actions = "read,write"; if (f.canWrite()) { System.out.println("File " + f.getPath() + " can be written"); return true; } else if (f.canRead()) { System.out.println("File " + f.getPath() + " can be read"); throw new PFFileServiceConnectionExceptionReadOnly("PATH: " + f.getPath()); } else { System.out.println("File " + f.getPath() + " can not be read"); throw new PFFileServiceConnectionExceptionAcsessDenied("PATH: " + f.getPath()); } /* try { AccessController.checkPermission(new FilePermission(path, actions)); System.out.println("You have read/write permition to use : " + path); return true; } catch (SecurityException e) { String actions2 = "read"; try { AccessController.checkPermission(new FilePermission(path, actions)); System.out.println("You have read permition to use : " + path); throw new PFFileServiceConnectionExceptionReadOnly(path); } catch (SecurityException ex) { System.out.println("You have no permition to use : " + path); throw new PFFileServiceConnectionExceptionAcsessDenied(ex.getLocalizedMessage()); } } // System.out.println("You have read/write permition to use : " + path); // return false; */ } //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. @Override public synchronized void setConfiguration(PFFileServiceConfig incomingConfig) { // no type checking here. Errors will occur if not passed the proper configuration file if (!(incomingConfig instanceof PFFileServiceLocalConfig)) { // log a warning so if somthing does go wrong we have a warning System.out.println("WARNING: an instance of PFFileConfigLocal was passed a configuration of type " + incomingConfig.getClass().getSimpleName() + ". The expected confiig type was an instance of PFFileServiceLocalConfiguration"); } config = (PFFileServiceLocalConfig) incomingConfig; } @Override public synchronized void configure() { // if we have no configuration make a default one if (config == null) { System.out.println("PFFileServiceLocal instance had no configuration. Generating default config"); config = new PFFileServiceLocalConfig(); } // configure the configuration config.configure(); } @Override public synchronized void mkdir(PFFolder directoy) { PFLocation location = new PFLocation(directoy); String rootDirectory = config.getRootDirectoryURL(); String locationString = location.getCannonicalPath(); String path = rootDirectory + locationString; System.out.println("Local File System mkdir PATH : " + path); File filedirectory = new File(path); boolean sucsess = filedirectory.mkdirs(); if (directoy.getName() == null) { throw new NullPointerException("The directory at location: " + filedirectory.getAbsolutePath() + " Could not be created becuase the name of the folder is (null)"); } if (sucsess == false && filedirectory.exists() == false) { throw new RuntimeException("THE DIRECTORY " + path + " COULD NOT BE CREATED"); } } @Override public synchronized void mkdirs(PFFolder directoy) { // // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. PFLocation location = new PFLocation(directoy); String rootDirectory = config.getRootDirectoryURL(); String locationString = location.toString(); String path = rootDirectory + locationString; System.out.println("PATH : " + path); File filedirectory = new File(path); boolean sucsess = filedirectory.mkdirs(); if (directoy.getName() == null) { throw new NullPointerException("The directory at location: " + filedirectory.getAbsolutePath() + " Could not be created becuase the name of the folder is (null)"); } if (sucsess == false && filedirectory.exists() == false) { throw new RuntimeException("THE DIRECTORY " + path + " COULD NOT BE CREATED"); } for (PFFolder folder : directoy.getFolders()) { this.mkdir(folder); } } @Override public synchronized void rmdir(PFFolder directory) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } public synchronized File[] ls(String path) { return new File(path).listFiles(); } @Override public synchronized PFFolder ls(PFLocation directory) { String path = convertPFLocationToPath(directory); return recursiveConvertDirectoryToPFFolder(new File(path)); } @Override public synchronized void writeStreamToLocation(BufferedOutputStream buf, PFLocation location) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } public synchronized String convertPFLocationToPath(PFLocation location) { if (location == null) { System.out.println("shkjfhskfjdhkljhsldfjshjdfhsj"); } return config.getRootDirectoryURL() + location.getCannonicalPath(); } /** * Returns an unlinked PFFolder that mimics the filestructure. This method is relativly quick to execute * @param file * @return */ private synchronized PFFolder recursiveConvertDirectoryToPFFolder(File directory) { PFFolder folder = new PFFolder(directory.getName()); for (File file : directory.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isFile(); } })) { PFGenericDocument document = new PFGenericDocument(); document.setName(file.getName()); folder.addDocument(document); } // get all the directories for (File subdirectory : directory.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } })) { PFFolder subfolder = recursiveConvertDirectoryToPFFolder(subdirectory); folder.addSubfolder(subfolder); } return folder; } /** * Resurns a PFFolder that mimics the filestructre and is linked into the database. Also automaticly adds sync objects to the folder. * @param file * @return */ private synchronized PFFolder recursiveConvertDirectoryToPFDatabaseFolder(File file) { return null; } /** * Removes a file from the server and into the recycle bin. * \warning This method is OS Specific * @param location */ @Override public synchronized void rm(PFLocation location) { File directoryOrFileToRemove = new File(convertPFLocationToPath(location)); if (directoryOrFileToRemove.exists()) { // move it to recyle bin instead of deleting if (config.isMac()) { } } } /** * Writes a folder to disk including any files inside. Will not modify any existing subfolders in the directory. Will not overide existing files. * @param folder */ @Override public synchronized void write(PFFolder folder) { // first makesure the directory exists File directory = new File(convertPFLocationToPath(folder.getLocation())); mkdirs(folder); } public synchronized void write(PFFile file) { PFLocation location = file.getLocation(); String locationPath = convertPFLocationToPath(location); File fileToWrite = new File(locationPath); while (file.exists()) { // while the file already exists // check if the content is the same. if it is, ignore the file write operation. if the content is diffrent, append a (1), (2)...(n) to the filename and try again // TODO - } } @Override public synchronized void write(PFFile file, PFFileAcsessOptions options) { // check for any unregognized file option perameters, log any unsupported oprtations checkForUnsupportedFileAcsessOptions(options); PFLocation location = file.getLocation(); String locationPath = convertPFLocationToPath(location); File fileToWrite = new File(locationPath); // first figure out if file already exists if (fileToWrite.exists()) { // here we process any options that have to do with writing similalr files for (PFFileAcsessOptions.option opt : options.getOptions()) { // increment a number at the end of the filename until you find a number that does exist, then set that as the new name for the document if (opt == PFFileAcsessOptions.option.kFileNameIncrementNumber) { int incrementNumber = 0; // the current increment that will be added to the begining of the filename String finalFilename = "(" + incrementNumber + ")" + file.getName(); // the final filename that will be used while (new File(convertPFLocationToPath(file.getParent().getLocation()) + File.pathSeparator + finalFilename).exists()) { finalFilename = "(" + incrementNumber + ")" + file.getName(); // the final filename that will be used incrementNumber++; } // set the name as the name of the file file.setName(finalFilename); continue; } } } else { try { // if the file does not already exist, just write the file FileWriter writer; BufferedWriter bufWrite; FileOutputStream out; location = file.getLocation(); locationPath = convertPFLocationToPath(location); fileToWrite = new File(locationPath); out = new FileOutputStream(fileToWrite); file.getData().reset(); int len = -1; byte[] bytes = new byte[32 * 1024]; len = file.getData().read(bytes); while (len != -1) { out.write(bytes, 0, len); len = file.getData().read(bytes); } out.close(); } catch (Exception ex) { throw new RuntimeException(ex); } } } @Override public void write(PFFolder folder, PFFileAcsessOptions options) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public PFFileAcsessOptions.option[] getSupportedFileAcsessOptions() { return new PFFileAcsessOptions.option[] { PFFileAcsessOptions.option.kFolderMerge, PFFileAcsessOptions.option.kFileNameIncrementNumber, PFFileAcsessOptions.option.kFileAcsessOptionWriteReplace }; } }