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.user.profile.impl.TerminalXmlFileProfilDataFactory;
00038 import org.openmobileis.common.util.PropertiesManager;
00039 import org.openmobileis.common.util.exception.ServiceException;
00040 import org.openmobileis.common.util.log.LogManager;
00041 import org.xmlpull.v1.XmlPullParserException;
00042 
00053 public class TerminalProfileManager {
00054 
00055   private static TerminalProfileManager manager;
00056 
00057   private String profilFile;
00058 
00059   private Profile profil = null;
00060 
00061   private TerminalProfileManager() {
00062     profilFile = PropertiesManager.getManager().getProperty("org.openmobileis.profil.file");
00063     if (profilFile == null) {
00064       profilFile = System.getProperty("user.dir") + File.separator + "WEB-INF" + File.separator + "conf" + File.separator + "properties" + File.separator
00065           + "profildata.xml";
00066     }
00067   }
00068 
00069   public static TerminalProfileManager getManager() {
00070     if (manager == null) {
00071       synchronized (org.openmobileis.modules.profiles.old.ProfileManager.class) {
00072         if (manager == null) {
00073           manager = new TerminalProfileManager();
00074           try {
00075             manager.parseFile();
00076           } catch (Throwable ex) {
00077             LogManager.traceError(0, ex);
00078           }
00079         }
00080       }
00081     }
00082     return manager;
00083   }
00084 
00085   public void saveProfilFile(String fileData) throws ServiceException {
00086     try {
00087       FileWriter out = new FileWriter(this.profilFile);
00088       try {
00089         out.write(fileData.toCharArray());
00090         out.flush();
00091       } finally {
00092         out.close();
00093       }
00094       this.parseFile();
00095     } catch (Throwable ex) {
00096       throw new ServiceException(ex);
00097     }
00098   }
00099 
00100   public Profile getProfil() {
00101     return profil;
00102   }
00103 
00104   private void parseFile() throws IOException, XmlPullParserException {
00105     try {
00106       FileReader reader = new FileReader(this.profilFile);
00107       try {
00108         TerminalXmlFileProfilDataFactory parser = new TerminalXmlFileProfilDataFactory(reader);
00109         this.profil = parser.getParsedProfil();
00110       } finally {
00111         reader.close();
00112       }
00113     } catch (FileNotFoundException ex) {
00114       LogManager.traceWarning(0, "Warning Profil file not found.");
00115     }
00116 
00117   }
00118 }

Generated on Mon Dec 4 11:03:30 2006 for OpenMobileIS by  doxygen 1.5.1-p1