TerminalProfileManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  
00023  *  Modifications :
00024  *  2006 Creation P.Delrieu
00025  * 
00026  */
00027 
00028 package org.openmobileis.module.profiles.terminal;
00029 
00030 import java.io.File;
00031 import java.io.FileNotFoundException;
00032 import java.io.FileReader;
00033 import java.io.FileWriter;
00034 import java.io.IOException;
00035 
00036 import org.openmobileis.common.user.profile.Profile;
00037 import org.openmobileis.common.util.PropertiesManager;
00038 import org.openmobileis.common.util.exception.ServiceException;
00039 import org.openmobileis.common.util.log.LogManager;
00040 import org.xmlpull.v1.XmlPullParserException;
00041 
00052 public class TerminalProfileManager {
00053 
00054   private static TerminalProfileManager manager;
00055 
00056   private String profilFile;
00057 
00058   private TerminalProfile profil = null;
00059 
00060   private TerminalProfileManager() {
00061     profilFile = PropertiesManager.getManager().getProperty("org.openmobileis.profil.file");
00062     if (profilFile == null) {
00063       profilFile = System.getProperty("user.dir") + File.separator + "WEB-INF" + File.separator + "conf" + File.separator
00064           + "profildata.xml";
00065     }
00066   }
00067   
00068   public String getProfileFilename()    {
00069         return this.profilFile;
00070   }
00071 
00072   public static TerminalProfileManager getManager() {
00073     if (manager == null) {
00074       synchronized (TerminalProfileManager.class) {
00075         if (manager == null) {
00076           manager = new TerminalProfileManager();
00077           try {
00078             manager.parseFile();
00079           } catch (Throwable ex) {
00080             LogManager.traceError(0, ex);
00081           }
00082         }
00083       }
00084     }
00085     return manager;
00086   }
00087 
00088   public void saveProfilFile(String fileData) throws ServiceException {
00089     try {
00090       FileWriter out = new FileWriter(this.profilFile);
00091       try {
00092         out.write(fileData.toCharArray());
00093         out.flush();
00094       } finally {
00095         out.close();
00096       }
00097       this.parseFile();
00098     } catch (Throwable ex) {
00099       throw new ServiceException(ex);
00100     }
00101   }
00102 
00103   public TerminalProfile getProfil() {
00104     return profil;
00105   }
00106 
00107   private void parseFile() throws IOException, XmlPullParserException {
00108     try {
00109         FileReader reader = new FileReader(this.profilFile);
00110       try {
00111         TerminalXmlProfilParser parser = new TerminalXmlProfilParser(reader);
00112         this.profil = parser.parse();
00113       } finally {
00114         reader.close();
00115       }
00116     } catch (FileNotFoundException ex) {
00117       LogManager.traceWarning(0, "Warning Profil file not found.");
00118     }
00119 
00120   }
00121 }

Generated on Mon Jan 11 21:19:17 2010 for OpenMobileIS by  doxygen 1.5.4