001/* 002 * Copyright (c) 2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2013-02-21 21:35:22 +0000 (Thu, 21 Feb 2013) $' 007 * '$Revision: 31480 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030/** 031 * 032 */ 033package org.kepler.objectmanager.library; 034 035import java.io.File; 036import java.io.FileInputStream; 037import java.io.FileNotFoundException; 038import java.io.FileOutputStream; 039import java.io.IOException; 040import java.io.InputStream; 041import java.io.ObjectInput; 042import java.io.ObjectInputStream; 043import java.io.ObjectOutputStream; 044import java.io.OutputStream; 045import java.io.Serializable; 046import java.util.Vector; 047 048import org.apache.commons.logging.Log; 049import org.apache.commons.logging.LogFactory; 050import org.kepler.util.DotKeplerManager; 051 052/** 053 * @author Aaron Schultz 054 */ 055public class LibSearchConfiguration implements Serializable { 056 private static final Log log = LogFactory.getLog(LibSearchConfiguration.class 057 .getName()); 058 private static final boolean isDebugging = log.isDebugEnabled(); 059 060 private Vector<Integer> _searchTypes; 061 062 private String _saveFileName; 063 064 public LibSearchConfiguration () { 065 066 // Set up file name for serializing to disk 067 File modDir = DotKeplerManager.getInstance() 068 .getTransientModuleDirectory("core"); 069 if (modDir != null) { 070 _saveFileName = modDir.toString(); 071 } else { 072 _saveFileName = DotKeplerManager.getInstance().getCacheDirString(); 073 } 074 if (!_saveFileName.endsWith(File.separator)) { 075 _saveFileName += File.separator; 076 } 077 _saveFileName += "ComponentSearchConfig"; 078 079 init(); 080 } 081 082 private void init() { 083 File saveFile = new File(_saveFileName); 084 085 if (saveFile.exists()) { 086 if (isDebugging) { 087 log.debug("Save file exists: " + saveFile.toString()); 088 } 089 090 try { 091 InputStream is = null; 092 ObjectInput oi = null; 093 try { 094 is = new FileInputStream(saveFile); 095 oi = new ObjectInputStream(is); 096 Object newObj = oi.readObject(); 097 _searchTypes = (Vector<Integer>) newObj; 098 return; 099 } finally { 100 if(oi != null) { 101 oi.close(); 102 } 103 if(is != null) { 104 is.close(); 105 } 106 } 107 108 } catch (Exception e1) { 109 // problem reading file, try to delete it 110 log.warn("Exception while reading save file: " 111 + e1.getMessage()); 112 try { 113 saveFile.delete(); 114 } catch (Exception e2) { 115 log.warn("Unable to delete save file: " 116 + e2.getMessage()); 117 } 118 } 119 } else { 120 setDefaults(); 121 } 122 123 } 124 125 public void addSearchType(int type) { 126 _searchTypes.add(new Integer(type)); 127 } 128 129 public void removeSearchType(int type) { 130 _searchTypes.remove(new Integer(type)); 131 } 132 133 public boolean contains(int type) { 134 if (_searchTypes.contains(new Integer(type))) { 135 return true; 136 } 137 return false; 138 } 139 140 public Vector<Integer> getSearchTypes() { 141 return _searchTypes; 142 } 143 144 private void setDefaults() { 145 _searchTypes = new Vector<Integer>(2); 146 _searchTypes.add(new Integer(LibSearch.TYPE_NAME)); 147 _searchTypes.add(new Integer(LibSearch.TYPE_ONTCLASSNAME)); 148 } 149 150 public void serializeToDisk() { 151 if (isDebugging) 152 log.debug("serializeToDisk()"); 153 154 File saveFile = new File(_saveFileName); 155 if (saveFile.exists()) { 156 if (isDebugging) 157 log.debug("delete " + saveFile); 158 saveFile.delete(); 159 } 160 try { 161 ObjectOutputStream oos = null; 162 try { 163 OutputStream os = new FileOutputStream(saveFile); 164 oos = new ObjectOutputStream(os); 165 oos.writeObject(_searchTypes); 166 oos.flush(); 167 if (isDebugging) { 168 log.debug("wrote " + saveFile); 169 } 170 } finally { 171 if(oos != null) { 172 oos.close(); 173 } 174 } 175 } catch (FileNotFoundException e) { 176 e.printStackTrace(); 177 } catch (IOException e) { 178 e.printStackTrace(); 179 } 180 181 } 182 183}