KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > backup > BackupManager


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2005 Emic Networks.
4  * Contact: c-jdbc@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): ______________________.
22  */

23
24 package org.objectweb.cjdbc.controller.backup;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.objectweb.cjdbc.common.exceptions.BackupException;
31 import org.objectweb.cjdbc.common.log.Trace;
32 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
33 import org.objectweb.cjdbc.common.xml.XmlComponent;
34
35 /**
36  * This class defines a BackupManager that is responsible for registering
37  * backupers and retrieving them as needed for backup/restore operations.
38  *
39  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet</a>
40  * @version 1.0
41  */

42 public class BackupManager implements XmlComponent
43 {
44   static Trace logger = Trace.getLogger(BackupManager.class.getName());
45
46   /**
47    * This is a HashMap of backuperName -> Backuper HashMap<String,Backuper>
48    */

49   private HashMap JavaDoc backupers;
50
51   /**
52    * Creates a new <code>BackupManager</code> object
53    */

54   public BackupManager()
55   {
56     backupers = new HashMap JavaDoc();
57   }
58
59   /**
60    * Retrieve a backuper given its name. If the backuper has not been registered
61    * null is returned.
62    *
63    * @param name the backuper to look for
64    * @return the backuper or null if not found
65    */

66   public synchronized Backuper getBackuperByName(String JavaDoc name)
67   {
68     return (Backuper) backupers.get(name);
69   }
70
71   /**
72    * Get the names of the <code>Backupers</code> available from this
73    * <code>BackupManager</code>.
74    *
75    * @return an (possibly 0-sized) array of <code>String</code> representing
76    * the name of the <code>Backupers</code>
77    */

78   public synchronized String JavaDoc[] getBackuperNames()
79   {
80     Set JavaDoc backuperNames = backupers.keySet();
81     return (String JavaDoc[]) backuperNames.toArray(new String JavaDoc[backuperNames.size()]);
82   }
83
84   /**
85    * Get the first backuper that supports the given dump format. If no backuper
86    * supporting that format can be found, null is returned.
87    *
88    * @param format the dump format that the backuper must handle
89    * @return a backuper or null if not found
90    */

91   public synchronized Backuper getBackuperByFormat(String JavaDoc format)
92   {
93     if (format == null)
94       return null;
95     for (Iterator JavaDoc iter = backupers.values().iterator(); iter.hasNext();)
96     {
97       Backuper b = (Backuper) iter.next();
98       if (format.equals(b.getDumpFormat()))
99         return b;
100     }
101     return null;
102   }
103
104   /**
105    * Register a new backuper under a logical name.
106    *
107    * @param name backuper logical name
108    * @param backuper the backuper instance
109    * @throws BackupException if a backuper is null or a backuper has already
110    * been registered with the given name.
111    */

112   public synchronized void registerBackuper(String JavaDoc name, Backuper backuper)
113       throws BackupException
114   {
115     if (backupers.containsKey(name))
116       throw new BackupException(
117           "A backuper has already been registered with name " + name);
118     if (backuper == null)
119       throw new BackupException(
120           "Trying to register a null backuper under name " + name);
121
122     if (logger.isInfoEnabled())
123       logger.info("Registering backuper " + name + " to handle format "
124           + backuper.getDumpFormat());
125
126     backupers.put(name, backuper);
127   }
128
129   /**
130    * Unregister a Backuper given its logical name.
131    *
132    * @param name the name of the backuper to unregister
133    * @return true if the backuper was removed successfully, false if it was not
134    * registered
135    */

136   public synchronized boolean unregisterBackuper(String JavaDoc name)
137   {
138     Object JavaDoc backuper = backupers.remove(name);
139
140     if (logger.isInfoEnabled() && (backuper != null))
141       logger.info("Unregistering backuper " + name + " that handled format "
142           + ((Backuper) backuper).getDumpFormat());
143
144     return backuper != null;
145   }
146
147   /**
148    * @see org.objectweb.cjdbc.common.xml.XmlComponent#getXml()
149    */

150   public synchronized String JavaDoc getXml()
151   {
152     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<" + DatabasesXmlTags.ELT_Backup + "> ");
153     for (Iterator JavaDoc iter = backupers.keySet().iterator(); iter.hasNext();)
154     {
155       String JavaDoc backuperName = (String JavaDoc) iter.next();
156       Backuper b = (Backuper) backupers.get(backuperName);
157       sb.append("<" + DatabasesXmlTags.ELT_Backuper + " "
158           + DatabasesXmlTags.ATT_backuperName + "=\"" + backuperName + "\" "
159           + DatabasesXmlTags.ATT_className + "=\"" + b.getClass() + "\" "
160           + DatabasesXmlTags.ATT_options + "=\"" + b.getOptions() + "\" />");
161     }
162     sb.append("</" + DatabasesXmlTags.ELT_Backup + ">");
163     return sb.toString();
164   }
165 }
Popular Tags