001 /* 002 * SessionWriter.java 003 * 004 * Created on September 10, 2004, 4:26 PM 005 * 006 * This file is part of the STAR Scheduler. 007 * Copyright (c) 2003-2006 STAR Collaboration - Brookhaven National Laboratory 008 * 009 * STAR Scheduler is free software; you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation; either version 2 of the License, or 012 * (at your option) any later version. 013 * 014 * STAR Scheduler is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with STAR Scheduler; if not, write to the Free Software 021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 */ 023 package gov.bnl.star.offline.scheduler.util.persistent; 024 025 026 import gov.bnl.star.offline.scheduler.SchedulerCommandLine; 027 import java.beans.XMLEncoder; 028 import java.beans.PersistenceDelegate; 029 import java.beans.Expression; 030 import java.beans.Encoder; 031 import java.io.BufferedOutputStream; 032 import java.io.FileOutputStream; 033 import java.net.URL; 034 import java.net.URI; 035 import java.io.PrintStream; 036 037 import gov.bnl.star.offline.scheduler.request.Request; 038 039 /** 040 * Converts a request object in to a session.xml file. 041 * @author Levente Hajdu 042 */ 043 public class SessionWriter { 044 045 /** Creates a new instance of SessionWriter */ 046 public SessionWriter(Request request) { 047 048 request.setSimulation(false); 049 050 try{ 051 052 String sessionFile = request.getID() + ".session.xml" ; 053 054 055 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream( sessionFile )); 056 PrintStream out = new PrintStream ( bufferedOutputStream); 057 XMLEncoder e = new XMLEncoder(bufferedOutputStream); 058 059 //PersistenceDelegate for URL class ~ This tells XMLEncoder how to deal with these objects 060 e.setPersistenceDelegate(URL.class, 061 new PersistenceDelegate() { 062 protected Expression instantiate(Object oldInstance,Encoder out) { 063 return new Expression(oldInstance,oldInstance.getClass(),"new", new Object[]{ oldInstance.toString() });}}); 064 065 e.setPersistenceDelegate(URI.class, 066 new PersistenceDelegate() { 067 protected Expression instantiate(Object oldInstance,Encoder out) { 068 return new Expression(oldInstance,oldInstance.getClass(),"new", new Object[]{ oldInstance.toString() });}}); 069 070 e.writeObject(request); 071 e.close(); 072 073 074 075 //write version number 076 bufferedOutputStream = new BufferedOutputStream(new FileOutputStream( sessionFile , true)); //we need to open it one more time to add the version number of the version of SUMS the file was written from. 077 out = new PrintStream ( bufferedOutputStream); 078 079 out.println("<!-- SUMS_Version_String=\""+ SchedulerCommandLine.getProgramName() + "\" -->"); 080 out.flush(); 081 out.close(); 082 083 084 } catch (Exception e) { 085 System.out.println("Error with XMLEncoder !!! \nCould not write persistent session : " + request.getID() + ".session.xml !!!\n" + e.toString()); 086 087 } 088 089 090 } 091 092 }