Source code: de/laures/cewolf/taglib/util/KeyGenerator.java
1 /* ================================================================
2 * Cewolf : Chart enabling Web Objects Framework
3 * ================================================================
4 *
5 * Project Info: http://cewolf.sourceforge.net
6 * Project Lead: Guido Laures (guido@laures.de);
7 *
8 * (C) Copyright 2002, by Guido Laures
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 package de.laures.cewolf.taglib.util;
24
25 import java.io.IOException;
26 import java.io.Serializable;
27 import java.rmi.MarshalledObject;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33 * @author Guido Laures
34 */
35 public abstract class KeyGenerator {
36
37 private static final Log log = LogFactory.getLog(KeyGenerator.class);
38
39 private static class NoKeyException extends RuntimeException {
40 public NoKeyException(String msg){
41 super(msg);
42 }
43 }
44
45 public static int generateKey(Serializable obj) {
46 if (obj == null) {
47 NoKeyException ex = new NoKeyException("assertion failed: can not generate key for null,");
48 throw ex;
49 }
50 try {
51 MarshalledObject mo = new MarshalledObject(obj);
52 return mo.hashCode();
53 } catch (IOException ioex) {
54 log.error("IOException during key generation KeyGenerator.generateKey()", ioex);
55 throw new NoKeyException(obj + " is not serializable.");
56 }
57 }
58 }