Author: clopes
Date: 2012-01-31 11:43:40 -0800 (Tue, 31 Jan 2012)
New Revision: 28166

Added:
   
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/SessionHandler.java
   
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/io/
   
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/io/SessionStateIO.java
Removed:
   
core3/impl/trunk/session-impl/src/main/java/org/cytoscape/session/internal/CysessionFactory.java
   
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/SessionShutdownHandler.java
Log:
Bug #649 : Remove the Cysession class from the API (missing changes)

Deleted: 
core3/impl/trunk/session-impl/src/main/java/org/cytoscape/session/internal/CysessionFactory.java
===================================================================
--- 
core3/impl/trunk/session-impl/src/main/java/org/cytoscape/session/internal/CysessionFactory.java
    2012-01-31 19:41:31 UTC (rev 28165)
+++ 
core3/impl/trunk/session-impl/src/main/java/org/cytoscape/session/internal/CysessionFactory.java
    2012-01-31 19:43:40 UTC (rev 28166)
@@ -1,196 +0,0 @@
-/*
-  Copyright (c) 2010, The Cytoscape Consortium (www.cytoscape.org)
-
-  The Cytoscape Consortium is:
-  - Institute for Systems Biology
-  - University of California San Diego
-  - Memorial Sloan-Kettering Cancer Center
-  - Institut Pasteur
-  - Agilent Technologies
-
-  This library is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2.1 of the License, or
-  any later version.
-
-  This library is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
-  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
-  documentation provided hereunder is on an "as is" basis, and the
-  Institute for Systems Biology and the Whitehead Institute
-  have no obligations to provide maintenance, support,
-  updates, enhancements or modifications.  In no event shall the
-  Institute for Systems Biology and the Whitehead Institute
-  be liable to any party for direct, indirect, special,
-  incidental or consequential damages, including lost profits, arising
-  out of the use of this software and its documentation, even if the
-  Institute for Systems Biology and the Whitehead Institute
-  have been advised of the possibility of such damage.  See
-  the GNU Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with this library; if not, write to the Free Software Foundation,
-  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
-*/
-package org.cytoscape.session.internal;
-
-import java.util.List;
-import java.util.Set;
-
-import org.cytoscape.model.CyNetwork;
-import org.cytoscape.model.CyNetworkManager;
-import org.cytoscape.model.subnetwork.CySubNetwork;
-import org.cytoscape.property.session.Cysession;
-import org.cytoscape.property.session.Cytopanel;
-import org.cytoscape.property.session.Cytopanels;
-import org.cytoscape.property.session.Desktop;
-import org.cytoscape.property.session.Network;
-import org.cytoscape.property.session.NetworkTree;
-import org.cytoscape.property.session.ObjectFactory;
-import org.cytoscape.property.session.OntologyServer;
-import org.cytoscape.property.session.Parent;
-import org.cytoscape.property.session.Server;
-import org.cytoscape.property.session.SessionState;
-import org.cytoscape.view.model.CyNetworkView;
-import org.cytoscape.view.model.CyNetworkViewManager;
-import org.cytoscape.view.vizmap.VisualMappingManager;
-
-public class CysessionFactory {
-
-       private static final String DEFAULT_SESSION_NOTE = "You can add note 
for this session here.";
-       
-       private final CyNetworkManager netMgr;
-       private final CyNetworkViewManager nvMgr;
-       private final VisualMappingManager vmMgr;
-       private final ObjectFactory factory;
-
-       public CysessionFactory(final CyNetworkManager netMgr,
-                                                       final 
CyNetworkViewManager nvMgr,
-                                                       final 
VisualMappingManager vmMgr) {
-               this.netMgr = netMgr;
-               this.nvMgr = nvMgr;
-               this.vmMgr = vmMgr;
-               this.factory = new ObjectFactory();
-       }
-
-       public Cysession createDefaultCysession() {
-               return createCysession(null, null, null);
-       }
-
-       /**
-        * Create a Cysession object from the current Cytoscape session 
attributes.
-        */
-       public Cysession createCysession(Desktop desktop, List<Cytopanel> 
cytopanels, String note) {
-               Cysession session = null;
-
-               // Initialize objects for the marshaller:
-               session = factory.createCysession();
-
-               if (note == null) note = DEFAULT_SESSION_NOTE;
-               session.setSessionNote(note);
-
-               SessionState sState = factory.createSessionState();
-               session.setSessionState(sState);
-
-               if (desktop == null) desktop = factory.createDesktop();
-               sState.setDesktop(desktop);
-
-               Cytopanels cps = getCytoPanelStates(cytopanels);
-               sState.setCytopanels(cps);
-               sState.setServer(getServerState());
-
-               NetworkTree netTree = getNetworkTree();
-               session.setNetworkTree(netTree);
-
-               return session;
-       }
-
-       /**
-        * Check loaded ontologies and save those states in cysession.xml.
-        * @return Server object
-        */
-       private Server getServerState() {
-               Server server = factory.createServer();
-               OntologyServer os = factory.createOntologyServer();
-
-               // We've omitted the concept of Ontology from the core api in 
3.0,
-               // so that code will exist in the future as a app (although 
with a public API)
-               //              Set<String> ontoNames = 
Cytoscape.getOntologyServer().getOntologyNames();
-               //              Map<String, URL> sources = 
Cytoscape.getOntologyServer().getOntologySources();
-               //
-               //              for (String name : ontoNames) {
-               //                      Ontology onto = 
factory.createOntology();
-               //                      onto.setName(name);
-               //                      
onto.setHref(sources.get(name).toString());
-               //                      os.getOntology().add(onto);
-               //              }
-
-               server.setOntologyServer(os);
-
-               return server;
-       }
-
-       /**
-        * Extract states of the 3 Cytopanels.
-        *
-        * @return
-        * Note: We will store the states of apps near future. The location of
-        * those states will be stored here.
-        */
-       private Cytopanels getCytoPanelStates(List<Cytopanel> cytopanels) {
-               Cytopanels cps = factory.createCytopanels();
-
-               if (cytopanels != null) {
-                       List<Cytopanel> cytoPanelList = cps.getCytopanel();
-                       for (Cytopanel panel : cytopanels) 
cytoPanelList.add(panel);
-               }
-
-               return cps;
-       }
-
-       private NetworkTree getNetworkTree() {
-               NetworkTree tree = factory.createNetworkTree();
-               Set<CyNetwork> networks = netMgr.getNetworkSet();
-               
-               if (networks != null) {
-                       final String DEF_STYLE_NAME = 
vmMgr.getDefaultVisualStyle().getTitle();
-                       
-                       for (CyNetwork cyNet : networks) {
-                               String name = 
cyNet.getRow(cyNet).get(CyNetwork.NAME, String.class);
-                               String styleName = DEF_STYLE_NAME;
-                               
-                               CyNetworkView cyNetView = 
nvMgr.getNetworkView(cyNet);
-                               
-                               if (cyNetView != null) {
-                                       org.cytoscape.view.vizmap.VisualStyle 
style = vmMgr.getVisualStyle(cyNetView);
-                                       if (style != null) styleName = 
style.getTitle();
-                               }
-
-                               Network net = new Network();
-                               net.setId(name);
-                               net.setFilename(name + ".xgmml");
-                               net.setVisualStyle(styleName);
-                               
net.setViewAvailable(nvMgr.getNetworkView(cyNet) != null);
-                               net.setVisualStyle(styleName);
-
-                               if (cyNet instanceof CySubNetwork) {
-                                       CySubNetwork cysSubNet = (CySubNetwork) 
cyNet;
-                                       String parentName = 
cysSubNet.getRootNetwork().getRow(cysSubNet.getRootNetwork()).get(CyNetwork.NAME,
 String.class);
-
-                                       Parent parent = new Parent();
-                                       parent.setId(parentName);
-
-                                       net.setParent(parent);
-                               }
-
-                               // We don't need to save selected nodes/edges 
anymore, because this info is saved in the CyTables now!
-                               //                              
net.setSelectedNodes(selNodes);
-                               //                              
net.setSelectedEdges(selEdges);
-
-                               tree.getNetwork().add(net);
-                       }
-               }
-
-               return tree;
-       }
-}

Copied: 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/SessionHandler.java
 (from rev 28154, 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/SessionShutdownHandler.java)
===================================================================
--- 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/SessionHandler.java
                            (rev 0)
+++ 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/SessionHandler.java
    2012-01-31 19:43:40 UTC (rev 28166)
@@ -0,0 +1,261 @@
+/*
+ Copyright (c) 2006, The Cytoscape Consortium (www.cytoscape.org)
+
+ The Cytoscape Consortium is:
+ - Institute for Systems Biology
+ - University of California San Diego
+ - Memorial Sloan-Kettering Cancer Center
+ - Institut Pasteur
+ - Agilent Technologies
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ any later version.
+
+ This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+ documentation provided hereunder is on an "as is" basis, and the
+ Institute for Systems Biology and the Whitehead Institute
+ have no obligations to provide maintenance, support,
+ updates, enhancements or modifications.  In no event shall the
+ Institute for Systems Biology and the Whitehead Institute
+ be liable to any party for direct, indirect, special,
+ incidental or consequential damages, including lost profits, arising
+ out of the use of this software and its documentation, even if the
+ Institute for Systems Biology and the Whitehead Institute
+ have been advised of the possibility of such damage.  See
+ the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+package org.cytoscape.internal;
+
+import java.io.File;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.swing.JInternalFrame;
+import javax.swing.JOptionPane;
+
+import org.cytoscape.application.events.CyShutdownEvent;
+import org.cytoscape.application.events.CyShutdownListener;
+import org.cytoscape.application.swing.CytoPanel;
+import org.cytoscape.application.swing.CytoPanelName;
+import org.cytoscape.application.swing.CytoPanelState;
+import org.cytoscape.internal.io.Cytopanel;
+import org.cytoscape.internal.io.Cytopanels;
+import org.cytoscape.internal.io.NetworkFrame;
+import org.cytoscape.internal.io.NetworkFrames;
+import org.cytoscape.internal.io.SessionState;
+import org.cytoscape.internal.io.SessionStateIO;
+import org.cytoscape.internal.view.CytoscapeDesktop;
+import org.cytoscape.internal.view.NetworkViewManager;
+import org.cytoscape.model.CyNetworkManager;
+import org.cytoscape.session.CySession;
+import org.cytoscape.session.events.SessionAboutToBeSavedEvent;
+import org.cytoscape.session.events.SessionAboutToBeSavedListener;
+import org.cytoscape.session.events.SessionLoadedEvent;
+import org.cytoscape.session.events.SessionLoadedListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SessionHandler implements CyShutdownListener, 
SessionLoadedListener, SessionAboutToBeSavedListener {
+
+       private static final String APP_NAME = 
"org.cytoscape.swing-application";
+       private static final String SESSION_STATE_FILENAME = 
"session_state.xml";
+       
+       private final CytoscapeDesktop desktop;
+       private final CyNetworkManager netMgr;
+       private final NetworkViewManager netViewMgr;
+       private final SessionStateIO sessionStateIO;
+       
+       private final Map<String, CytoPanelName> CYTOPANEL_NAMES = new 
LinkedHashMap<String, CytoPanelName>();
+       
+       private static final Logger logger = 
LoggerFactory.getLogger(SessionHandler.class);
+       
+       public SessionHandler(final CytoscapeDesktop desktop,
+                                                 final CyNetworkManager netMgr,
+                                                 final NetworkViewManager 
netViewMgr,
+                                                 final SessionStateIO 
sessionStateIO) {
+               this.desktop = desktop;
+               this.netMgr = netMgr;
+               this.netViewMgr = netViewMgr;
+               this.sessionStateIO = sessionStateIO;
+               
+               CYTOPANEL_NAMES.put("CytoPanel1", CytoPanelName.WEST);
+               CYTOPANEL_NAMES.put("CytoPanel2", CytoPanelName.SOUTH);
+               CYTOPANEL_NAMES.put("CytoPanel3", CytoPanelName.EAST);
+       }
+
+       @Override
+       public void handleEvent(CyShutdownEvent e) {
+               // If there are no networks, just quit.
+               if (netMgr.getNetworkSet().size() == 0) 
+                       return;
+
+               // Ask user whether to save current session or not.
+               final String msg = "Do you want to save your session?";
+               final String header = "Save Networks Before Quitting?";
+               final Object[] options = { "Yes, save and quit", "No, just 
quit", "Cancel" };
+               final int n = JOptionPane.showOptionDialog(desktop.getJFrame(), 
msg, header,
+                                                    JOptionPane.YES_NO_OPTION,
+                                                    
JOptionPane.QUESTION_MESSAGE, 
+                                                                               
         null, options, options[0]);
+
+               if (n == JOptionPane.NO_OPTION) {
+                       return;
+               } else if (n == JOptionPane.YES_OPTION) {
+                       // TODO 
+                       System.out.println("SESSION SAVING NOT IMPLEMENTED 
!!!!");
+                       return;
+               } else {
+                       e.abortShutdown("User canceled the shutdown request.");
+                       return; 
+               }
+       }
+
+       @Override
+       public void handleEvent(SessionAboutToBeSavedEvent e) {
+               final SessionState sessState = new SessionState();
+               
+               // Network Frames
+               final NetworkFrames netFrames = new NetworkFrames();
+               sessState.setNetworkFrames(netFrames);
+               
+               final JInternalFrame[] internalFrames = 
netViewMgr.getDesktopPane().getAllFrames();
+       
+       for (JInternalFrame iframe : internalFrames) {
+               final NetworkFrame nf = new NetworkFrame();
+               
+            nf.setFrameID(iframe.getTitle());
+            nf.setX(BigInteger.valueOf(iframe.getX()));
+            nf.setY(BigInteger.valueOf(iframe.getY()));
+               
+            netFrames.getNetworkFrame().add(nf);
+       }
+               
+        // CytoPanels States
+       final Cytopanels cytopanels = new Cytopanels();
+               sessState.setCytopanels(cytopanels);
+               
+               for (Map.Entry<String, CytoPanelName> entry : 
CYTOPANEL_NAMES.entrySet()) {
+                       final CytoPanel p = 
desktop.getCytoPanel(entry.getValue());
+                       
+                       final Cytopanel cytopanel = new Cytopanel();
+                       cytopanel.setId(entry.getKey());
+                       cytopanel.setPanelState(p.getState().toString());
+                       
cytopanel.setSelectedPanel(Integer.toString(p.getSelectedIndex()));
+                       
+                       cytopanels.getCytopanel().add(cytopanel);
+               }
+               
+               // Create temp file
+               File tmpFile = new File(System.getProperty("java.io.tmpdir"), 
SESSION_STATE_FILENAME);
+               tmpFile.deleteOnExit();
+               
+               // Write to the file
+               sessionStateIO.write(sessState, tmpFile);
+               
+               // Add it to the apps list
+               List<File> fileList = new ArrayList<File>();
+               fileList.add(tmpFile);
+               
+               try {
+                       e.addAppFiles(APP_NAME, fileList);
+               } catch (Exception ex) {
+                       logger.error("Error adding "+SESSION_STATE_FILENAME+" 
file to be saved in the session.", ex);
+               }
+       }
+
+       @Override
+       public void handleEvent(SessionLoadedEvent e) {
+               final CySession sess = e.getLoadedSession();
+
+               if (sess != null) {
+                       final Map<String, List<File>> filesMap = 
sess.getAppFileListMap();
+
+                       if (filesMap != null) {
+                               final List<File> files = filesMap.get(APP_NAME);
+
+                               if (files != null) {
+                                       SessionState sessState = null;
+                                       
+                                       for (File f : files) {
+                                               if 
(f.getName().endsWith(SESSION_STATE_FILENAME)) {
+                                                       // There should be only 
one file!
+                                                       sessState = 
sessionStateIO.read(f);
+                                                       break;
+                                               }
+                                       }
+                                       
+                                       if (sessState != null) {
+                                               
setNetworkFrameLocations(sessState.getNetworkFrames());
+                                               
setCytoPanelStates(sessState.getCytopanels());
+                                       }
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Restore each network frame's location.
+        * @param frames
+        */
+       private void setNetworkFrameLocations(final NetworkFrames frames) {
+               if (frames != null) {
+                       final List<NetworkFrame> framesList = 
frames.getNetworkFrame();
+
+                       for (NetworkFrame nf : framesList) {
+                               String title = nf.getFrameID();
+                               JInternalFrame[] internalFrames = 
netViewMgr.getDesktopPane().getAllFrames();
+
+                               for (JInternalFrame iframe : internalFrames) {
+                                       if (iframe.getTitle() != null && 
iframe.getTitle().equals(title)
+                                                       && nf.getX() != null && 
nf.getY() != null) {
+                                               int x = nf.getX().intValue();
+                                               int y = nf.getY().intValue();
+                                               iframe.setLocation(x, y);
+                                       }
+                               }
+                       }
+               }
+       }
+       
+       /**
+        * Restore the states of the CytoPanels.
+        * @param cytopanels
+        */
+       private void setCytoPanelStates(final Cytopanels cytopanels) {
+               if (cytopanels != null) {
+                       final List<Cytopanel> cytopanelsList = 
cytopanels.getCytopanel();
+
+                       for (Cytopanel cytopanel : cytopanelsList) {
+                               String id = cytopanel.getId();
+                               final CytoPanelName panelName = 
CYTOPANEL_NAMES.get(id);
+
+                               if (panelName != null) {
+                                       final CytoPanel p = 
desktop.getCytoPanel(panelName);
+
+                                       try {
+                                               
p.setState(CytoPanelState.valueOf(cytopanel.getPanelState().toUpperCase().trim()));
+                                       } catch (Exception ex) {
+                                               logger.error("Cannot restore 
the state of panel \"" + panelName.getTitle() + "\"", ex);
+                                       }
+
+                                       try {
+                                               
p.setSelectedIndex(Integer.parseInt(cytopanel.getSelectedPanel()));
+                                       } catch (Exception ex) {
+                                               logger.error("Cannot restore 
the selected index of panel \"" + panelName.getTitle() + "\"", ex);
+                                       }
+                               }
+                       }
+               }
+       }
+}

Deleted: 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/SessionShutdownHandler.java
===================================================================
--- 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/SessionShutdownHandler.java
    2012-01-31 19:41:31 UTC (rev 28165)
+++ 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/SessionShutdownHandler.java
    2012-01-31 19:43:40 UTC (rev 28166)
@@ -1,89 +0,0 @@
-
-/*
- File: SessionShutdownHandler.java
-
- Copyright (c) 2006, The Cytoscape Consortium (www.cytoscape.org)
-
- The Cytoscape Consortium is:
- - Institute for Systems Biology
- - University of California San Diego
- - Memorial Sloan-Kettering Cancer Center
- - Institut Pasteur
- - Agilent Technologies
-
- This library is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2.1 of the License, or
- any later version.
-
- This library is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
- MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
- documentation provided hereunder is on an "as is" basis, and the
- Institute for Systems Biology and the Whitehead Institute
- have no obligations to provide maintenance, support,
- updates, enhancements or modifications.  In no event shall the
- Institute for Systems Biology and the Whitehead Institute
- be liable to any party for direct, indirect, special,
- incidental or consequential damages, including lost profits, arising
- out of the use of this software and its documentation, even if the
- Institute for Systems Biology and the Whitehead Institute
- have been advised of the possibility of such damage.  See
- the GNU Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with this library; if not, write to the Free Software Foundation,
- Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
- */
-
-package org.cytoscape.internal;
-
-
-//import org.cytoscape.application.swing.session.CySessionManager;
-import org.cytoscape.model.CyNetworkManager;
-
-import org.cytoscape.application.events.CyShutdownEvent;
-import org.cytoscape.application.events.CyShutdownListener;
-import org.cytoscape.application.swing.CySwingApplication;
-
-import javax.swing.JOptionPane;
-
-public class SessionShutdownHandler implements CyShutdownListener {
-
-//     private CySessionManager session;
-       private CySwingApplication desktop;
-       private CyNetworkManager netmgr;
-
-       public SessionShutdownHandler(/*CySessionManager session,*/ 
CySwingApplication desktop, CyNetworkManager netmgr) {
-               //this.session = session;
-               this.desktop = desktop;
-               this.netmgr = netmgr;
-       }
-
-       public void handleEvent(CyShutdownEvent e) {
-
-               // If there are no networks, just quit.
-               if (netmgr.getNetworkSet().size() == 0) 
-                       return;
-
-               // Ask user whether to save current session or not.
-               final String msg = "Do you want to save your session?";
-               final String header = "Save Networks Before Quitting?";
-               final Object[] options = { "Yes, save and quit", "No, just 
quit", "Cancel" };
-               final int n = JOptionPane.showOptionDialog(desktop.getJFrame(), 
msg, header,
-                                                    JOptionPane.YES_NO_OPTION,
-                                                    
JOptionPane.QUESTION_MESSAGE, 
-                                                                               
         null, options, options[0]);
-
-               if (n == JOptionPane.NO_OPTION) {
-                       return;
-               } else if (n == JOptionPane.YES_OPTION) {
-                       // TODO 
-                       System.out.println("SESSION SAVING NOT IMPLEMENTED 
!!!!");
-                       return;
-               } else {
-                       e.abortShutdown("User canceled the shutdown request.");
-                       return; 
-               }
-       }
-}

Added: 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/io/SessionStateIO.java
===================================================================
--- 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/io/SessionStateIO.java
                         (rev 0)
+++ 
core3/impl/trunk/swing-application-impl/src/main/java/org/cytoscape/internal/io/SessionStateIO.java
 2012-01-31 19:43:40 UTC (rev 28166)
@@ -0,0 +1,69 @@
+package org.cytoscape.internal.io;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Responsible for writing and reading session state info as private app files.
+ */
+public class SessionStateIO {
+       
+       private static final String SESSION_STATE_PACKAGE = 
SessionState.class.getPackage().getName();
+       private static final Logger logger = 
LoggerFactory.getLogger(SessionStateIO.class);
+
+       public SessionState read(final File file) {
+               SessionState sessionState = null;
+               InputStream is = null;
+               
+               try {
+                       is = new FileInputStream(file);
+                       final JAXBContext jaxbContext = 
JAXBContext.newInstance(SESSION_STATE_PACKAGE, getClass().getClassLoader());
+                       final Unmarshaller unmarshaller = 
jaxbContext.createUnmarshaller();
+                       sessionState = (SessionState) 
unmarshaller.unmarshal(is);
+               } catch (Exception e) {
+                       logger.error("SessionState Read error", e);
+               } finally {
+                       if (is != null) {
+                               try {
+                                       is.close();
+                               } catch (IOException ioe) {
+                               }
+                       }
+               }
+
+               return sessionState;
+       }
+
+       public void write(final SessionState sessionState, final File file) {
+               OutputStream out = null;
+
+               try {
+                       out = new FileOutputStream(file);
+                       final JAXBContext jc = 
JAXBContext.newInstance(SessionState.class.getPackage().getName(), 
this.getClass()
+                                       .getClassLoader());
+                       final Marshaller m = jc.createMarshaller();
+                       m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, 
Boolean.TRUE);
+                       m.marshal(sessionState, out);
+               } catch (Exception e) {
+                       logger.error("SessionState Write error", e);
+               } finally {
+                       if (out != null) {
+                               try {
+                                       out.close();
+                               } catch (IOException ioe) {
+                               }
+                       }
+               }
+       }
+}

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to