📄 serverimpl.java
字号:
/* * ServerImpl.java * * Created on 24. oktober 2005, 16:07 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package backup.server;import maca.*;import backup.client.*;import backup.model.*;import java.net.*;import java.util.*;import java.io.*;import java.rmi.*;import java.net.*;import java.rmi.registry.*;import java.rmi.Naming;import java.rmi.server.UnicastRemoteObject;import java.util.ArrayList;import java.sql.SQLException;import burningtools.MkIsoFsController;/** * * @author Kim Stenbo Nielsen */public final class ServerImpl extends UnicastRemoteObject implements Server { private static boolean RUNNING = false; private static final String SERVICE_NAME = "miniWebPACS_Backup"; private static String HOST_NAME; private static String HOST_ADDRESS; private static int RMI_PORT; Registry reg; private File ISORoot; private Properties property = new Properties(); private static Vector<BackupIso> isoFiles = new Vector<BackupIso>(); private Timer timer; private int timerInterval; private Date startTime; private long arciveSizeInBytes; private String defaultBackupLabel; private String defaultBackupLocation; private String defaultBackupDescription; private String defaultBackupMediumType; private Dao dao; private String dbServerName; private String dbServerPort; private String dbName; private String dbUsername; private String dbPassword; /** * CONSTRUCTORS: Creates a new instance of ServerImpl */ public ServerImpl() throws RemoteException { System.out.println("\n**********************************************"); System.out.println("** Welcome to miniWebPACS Backup Server **"); System.out.println("**********************************************\n"); System.out.println("Initializing ... "); //Loading properties from XML-file! loadConfiguration(); // Connect to database System.out.println(" - Creating Database Access Object"); try { dao = new Dao(dbServerName, dbServerPort, dbName, dbUsername, dbPassword); } catch (ClassNotFoundException e) { System.out.println("\tCould not find SQL driver!"); System.out.println("\t\"" + e + "\""); System.exit(1); } catch (InstantiationException e) { System.out.println("\tCould not load the SQL driver!"); System.out.println("\t\"" + e + "\""); System.exit(1); } catch (IllegalAccessException e) { System.out.println("\tIllegal action attempted!"); System.out.println("\t\"" + e + "\""); System.exit(1); } catch (SQLException e) { System.out.println("\tCould not connect to the database! Please make sure that the propor DB properties are set in the properties file."); System.out.println("\tDao returned:"); System.out.println("\t\"" + e + "\""); System.exit(1); } //Set the timer timer = new Timer(); timer.schedule(new BackupManager(), 1000); // wait 1 seconds before starting the first database check! try { HOST_NAME = InetAddress.getLocalHost().getHostName(); System.out.print(" - Host name is: " + HOST_NAME); HOST_ADDRESS = InetAddress.getByName(HOST_NAME).getHostAddress(); System.out.println(" (" + HOST_ADDRESS + ")"); System.out.println(" - Starting RMI registry on port " + RMI_PORT); reg = LocateRegistry.createRegistry(RMI_PORT); // Bind the serverobject to the rmi registry System.out.println(" - Binding backup service object in RMI registry"); Naming.rebind("rmi://" + HOST_ADDRESS + "/" + SERVICE_NAME, this); System.out.println("Backup Service running ...\n"); RUNNING = true; } catch (MalformedURLException e) { System.out.println("\tCould not resolve the local IP"); System.out.println("\t\"" + e + "\""); System.exit(1); } catch (java.net.UnknownHostException e) { System.out.println("\tCould not resolve the local IP"); System.out.println("\t\"" + e + "\""); System.exit(1); } catch (RemoteException e) { System.out.println("\tCould not bind server in RMI registry."); System.out.println("\tRMI error:"); System.out.println("\t\"" + e + "\""); System.exit(1); } } /** * MAIN-METHOD: * @param args the command line arguments */ public static void main(String[] arg) { if (RUNNING == false) { try { ServerImpl server = new ServerImpl(); //ServerSocket s = new ServerSocket(); } catch (RemoteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("An instance of BackupServer is already running..."); } } private void loadConfiguration() { File propertyFile = new File("miniwebpacs.xml"); if(propertyFile.exists()) { System.out.println("Reading custom proporties file!"); try { FileInputStream in = new FileInputStream(propertyFile); property.loadFromXML(in); //property.list(System.out); in.close(); } catch(IOException e) { System.out.println("Invilid property-file!!"); System.out.println("System terminates!"); System.exit(1); } } else { System.out.println("Property-file can not be found!!"); System.out.println("System terminates!"); System.exit(1); } // Properties of timer Calendar calendar = Calendar.getInstance(); int year = Integer.parseInt(property.getProperty("timerStartYear")); int month = Integer.parseInt(property.getProperty("timerStartMonth")); int date = Integer.parseInt(property.getProperty("timerStartDay")); int hourOfDay =Integer.parseInt(property.getProperty("timerStartHour")); int minute = Integer.parseInt(property.getProperty("timerStartMinute")); int second = Integer.parseInt(property.getProperty("timerStartSecond")); calendar.set(year, month, date, hourOfDay, minute, second); startTime = calendar.getTime(); //Get the timer inteval time. this.timerInterval = Integer.parseInt(property.getProperty("timerInterval")); // RMI proporties RMI_PORT = Integer.parseInt(property.getProperty("rmiPort")); // ISO proporties this.ISORoot = new File(property.getProperty("isoRoot")); this.arciveSizeInBytes = Integer.parseInt(property.getProperty("isoFileSize")) * 1048576 ; this.defaultBackupLabel = property.getProperty("backupLabel"); this.defaultBackupLocation = property.getProperty("backupLocation"); this.defaultBackupDescription = property.getProperty("backupDescription"); this.defaultBackupMediumType = property.getProperty("backupMediumType"); // DB proporties dbServerName = property.getProperty("dbServerName"); dbServerPort = property.getProperty("dbServerPort"); dbName = property.getProperty("dbName"); dbUsername = property.getProperty("dbUsername"); dbPassword = property.getProperty("dbPassword"); } /** * IMPLEMENTS: <<Interface Server>> * Defines methods from interface backup.server.Server */ public Vector<BackupIso> getFileList() { for (Object o : isoFiles.toArray()) { ((BackupIso) o).setLength(); } return isoFiles; } public boolean getFile(FileTarget target, BackupIso source) throws RemoteException { FileInputStream in; byte[] bytes = new byte[100000]; int bytesRead = 0; try{ in = new FileInputStream(source); while ((bytesRead = in.read(bytes)) != -1) { if (bytesRead == bytes.length) { target.write(bytes); } else { byte[] temp = new byte[bytesRead]; for (int i = 0; i<bytesRead; i++) { temp[i] = bytes[i]; } target.write(temp); } } return true; } catch (IOException e) { e.printStackTrace(); return false; } } public void confirmBackup(BackupIso iso) throws RemoteException { isoFiles.removeElement(iso); dao.updateBackupIso(iso); } public boolean login(FileClerc fc) { Authentication au = new Authentication(property); System.out.println("User '" + fc.getUsername() + "' requests access"); if (au.authenticate(fc.getUsername(), fc.getPassword())) { if (au.access_allowed("ConfirmacaoPacientes;Administracao;AcessoExterno","Execucao")) { System.out.println("Access granted!"); return true; } else { return false; } } else { System.out.println("User not recognized by MACA!"); return false; } } /** * INNER-CLASS: BackupManager * Defines timer */ class BackupManager extends TimerTask { Vector<Study> allStudies; public void run() { allStudies = dao.getStudiesToBackup(); boolean backupNeeded = getBackupNeed(arciveSizeInBytes); System.out.println("Checking database for new images (min size:" + arciveSizeInBytes +" Bytes): "); // Remove studies larger than the medium size from allStudies for (int i=(allStudies.size()-1); i>=0; i--) { if(allStudies.elementAt(i).getStudySize() > arciveSizeInBytes) { allStudies.remove(i); } } if (allStudies.isEmpty()) { System.out.println("There are studies ready for backup, but the backup medium choosen has a too low capasity!"); backupNeeded = false; } else { // Sort allStudies with the youngest on top and the oldest in the bottom for (int i=0; i<allStudies.size(); i++) { Study youngestStudy = allStudies.elementAt(i); for (int j=i; j<(allStudies.size()-1); j++) { if(allStudies.elementAt(i).getStudyTime() < allStudies.elementAt(i+1).getStudyTime()) { youngestStudy = allStudies.elementAt(i+1); } } allStudies.remove(youngestStudy); allStudies.insertElementAt(youngestStudy, i); } } BackupIso iso; while (backupNeeded) { // Create backupStudies -> Vector containing studies to be backed up. Priority oldest first! long spaceLeftOnIso = arciveSizeInBytes; Vector<Study> backupStudies = new Vector<Study>(); for (int i=(allStudies.size()-1); i>=0; i--) { if (allStudies.elementAt(i).getStudySize() < spaceLeftOnIso) { spaceLeftOnIso -= allStudies.elementAt(i).getStudySize(); backupStudies.addElement(allStudies.elementAt(i)); allStudies.remove(i); } } // Make ISO-file from BackupIso-object iso = new BackupIso(ISORoot + "/backup.iso", backupStudies); //TODO: implement option for changing iso file size! try{ iso.createNewFile(); } catch(IOException e){ e.printStackTrace(); } iso.setInfo(defaultBackupLabel, defaultBackupMediumType, defaultBackupDescription, defaultBackupLocation); //TODO: maybe this line is not necessary until confirm. int backupEntryIndex = dao.insertBackupIso(iso); String uniquePath = ISORoot + "/backup" + backupEntryIndex + ".iso"; iso = new BackupIso(uniquePath, iso.getStudies()); String uniqueBackupLabel = defaultBackupLabel + backupEntryIndex; iso.setBackupUniqueId(backupEntryIndex); iso.setInfo(uniqueBackupLabel, defaultBackupMediumType, defaultBackupDescription, defaultBackupLocation); //boolean wentWell = iso.renameTo(uniquePath); //System.out.println("File path updated: " + wentWell); isoFiles.addElement(iso); MkIsoFsController.getMkIsoFs().createIsoFile(iso); iso.setISOCreationDate(new Date()); backupNeeded = getBackupNeed(arciveSizeInBytes); } (new File(ISORoot + "/backup.iso")).delete(); // Delete temporary iso file. timer.schedule(new BackupManager(), timerInterval); // TODO: replace wait in 10 seconds with proporty } public boolean getBackupNeed(long arciveSize) { boolean backupNeeded = false; long totalStudySize = 0; for (int i=0; i<allStudies.size(); i++) { totalStudySize += allStudies.get(i).getStudySize(); } if (totalStudySize > arciveSize) { backupNeeded = true; } return backupNeeded; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -