Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: marauroa/game/RPObjectFactory.java


1   /* $Id: RPObjectFactory.java,v 1.9 2003/12/10 22:49:46 root777 Exp $ */
2   /***************************************************************************
3    *                      (C) Copyright 2003 - Marauroa                      *
4    ***************************************************************************
5    ***************************************************************************
6    *                                                                         *
7    *   This program is free software; you can redistribute it and/or modify  *
8    *   it under the terms of the GNU General Public License as published by  *
9    *   the Free Software Foundation; either version 2 of the License, or     *
10   *   (at your option) any later version.                                   *
11   *                                                                         *
12   ***************************************************************************/
13  package marauroa.game;
14  
15  import java.io.IOException;
16  import java.util.HashMap;
17  import java.util.Map;
18  import marauroa.game.RPObject;
19  import marauroa.marauroad;
20  import marauroa.net.InputSerializer;
21  import marauroa.net.OutputSerializer;
22  
23  /** This class is used to creating a RPObject object from a stream or to serialize
24   *  a RPObject object to a stream. */
25  public class RPObjectFactory
26  {
27    private static Map factoryArray;
28    private static RPObjectFactory rpObjectFactory;
29    
30    private RPObjectFactory()
31    {
32      factoryArray= new HashMap();
33    }
34    
35    /** This method returns an instance of RPObjectFactory
36     *  @return A shared instance of RPObjectFactory */
37    public static RPObjectFactory getFactory()
38    {
39      if(rpObjectFactory==null)
40      {
41        rpObjectFactory=new RPObjectFactory();
42        rpObjectFactory.register(0,RPObject.class);
43      }
44      
45      return rpObjectFactory;
46    }
47    
48    /** registers a rpObject class
49     @param index the RPObject type
50     @param rpObjectClass the RPObject class
51     **/
52    public void register(int index,Class rpObjectClass)
53    {
54      factoryArray.put(new Integer(index),rpObjectClass);
55    }
56    
57    /** Returns a object of the right class from a stream of serialized data.
58     @param data the serialized data
59     @throws IOException in case of problems with the RPObject */
60    public RPObject getRPObject(InputSerializer is) throws IOException
61    {
62      marauroad.trace("RPObjectFactory::getRPObject",">");
63      try
64      {
65        int index = is.readInt();
66        Class rpObjectType=(Class) factoryArray.get(new Integer(index));
67        if(rpObjectType!=null)
68        {
69          RPObject tmp=(RPObject) rpObjectType.newInstance();
70          tmp.readObject(is);
71          return tmp;
72        }
73        else
74        {
75          marauroad.trace("RPObjectFactory::getRPObject","X","RPObject type ["+index+"] is not registered.");
76          throw new IOException("RPObject type ["+index+"] is not registered.");
77        }
78      }
79      catch(Exception e)
80      {
81        e.printStackTrace(System.out);
82        marauroad.trace("RPObjectFactory::getRPObject","X",e.getMessage());
83        throw new IOException(e.getMessage());
84      }
85      finally
86      {
87        marauroad.trace("RPObjectFactory::getRPObject","<");
88      }
89    }
90    
91    /**
92     * adds the given RPObject into serializer
93     **/
94    public void addRPObject(OutputSerializer os,RPObject rp_object) throws IOException
95    {
96      marauroad.trace("RPObjectFactory::addRPObject",">");
97      try
98      {
99        int index = rp_object.objectType;
100       os.write(index);
101       rp_object.writeObject(os);
102     }
103     catch(Exception e)
104     {
105       marauroad.trace("RPObjectFactory::addRPObject","X",e.getMessage());
106       throw new IOException(e.getMessage());
107     }
108     finally
109     {
110       marauroad.trace("RPObjectFactory::addRPObject","<");
111     }
112   }
113 }
114 
115