KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > sessiondemo > ServerImpl


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.sessiondemo;
31
32 /**
33  ** Implementation of the OMG IDL3 <tt>Server</tt> component type.
34  **/

35 public class ServerImpl
36 extends Server_MainSegBase
37 implements java.io.Serializable JavaDoc
38 {
39     // server
40
static private Product[] _aproducts = {
41         new Product("A", 10),
42         new Product("B", 20),
43         new Product("C", 30),
44         new Product("D", 10),
45         new Product("E", 50),
46         new Product("F", 90),
47         new Product("G", 20),
48         new Product("H", 60),
49         new Product("I", 10),
50         new Product("J", 70),
51     };
52
53     static private java.util.Hashtable JavaDoc _products;
54
55     static {
56         _products = new java.util.Hashtable JavaDoc();
57         for (int i=0;i<_aproducts.length;i++) {
58             _products.put(_aproducts[i].id, _aproducts[i]);
59         }
60     }
61
62     transient private java.util.ArrayList JavaDoc _purchased;
63     private String JavaDoc _spurchased;
64
65     // default constructor
66
public
67     ServerImpl()
68     {
69         // server
70
_purchased = null;
71         _spurchased = null;
72     }
73
74     //
75
// overriden operations
76
//
77

78     final public void
79     ccm_activate()
80     throws org.omg.Components.CCMException
81     {
82         System.err.println("### [ServerImpl] ###");
83         System.err.println("### [ServerImpl] ccm_activate called");
84         System.err.println("### [ServerImpl] ###");
85
86         // NOTE: first activation, create array list
87
if (_purchased==null) {
88             _purchased = new java.util.ArrayList JavaDoc();
89         }
90
91         // NOTE: re-populate array list from saved data
92
if (_spurchased!=null) {
93             // break the string
94
String JavaDoc purchased = _spurchased;
95             int idx = purchased.indexOf(',');
96             String JavaDoc pid = null;
97             while (idx!=-1) {
98                 pid = purchased.substring(0, idx);
99                 purchased = purchased.substring(idx+1);
100                 idx = purchased.indexOf(',');
101                 _purchased.add(pid);
102             }
103
104             _purchased.add(purchased);
105             _spurchased = null;
106         }
107     }
108
109     final public void
110     ccm_passivate()
111     throws org.omg.Components.CCMException
112     {
113         System.err.println("### [ServerImpl] ###");
114         System.err.println("### [ServerImpl] ccm_passivate called");
115         System.err.println("### [ServerImpl] ###");
116
117         // NOTE: store purchased items ids in a string
118
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
119         String JavaDoc[] purchased = (String JavaDoc[])_purchased.toArray(new String JavaDoc[0]);
120         for (int i=0;i<purchased.length;i++) {
121             buffer.append(purchased[i]+",");
122         }
123
124         //
125
_spurchased = buffer.toString();
126     }
127
128     final public void
129     ccm_remove()
130     throws org.omg.Components.CCMException
131     {
132         System.err.println("### [ServerImpl] ###");
133         System.err.println("### [ServerImpl] ccm_remove called");
134         System.err.println("### [ServerImpl] ###");
135
136         _purchased.clear();
137         _purchased = null;
138         _spurchased = null;
139     }
140
141     //
142
// IDL:objectweb.org/ccm/sessiondemo/Server:1.0
143
//
144

145     //
146
// IDL:objectweb.org/ccm/sessiondemo/IServer:1.0
147
//
148

149     final public Product[]
150     list_products()
151     {
152         return _aproducts;
153     }
154
155     final public void
156     purchase_products(String JavaDoc[] pids)
157     {
158         _purchased.addAll(java.util.Arrays.asList(pids));
159     }
160
161     final public void
162     remove_products(String JavaDoc[] pids)
163     {
164         for (int i=0;i<pids.length;i++) {
165             for (int j=0;j<_purchased.size();j++) {
166                 if (pids[i].equals(_purchased.get(j))) {
167                     _purchased.remove(j);
168                     break;
169                 }
170             }
171         }
172     }
173
174     final public Product[]
175     view_caddie()
176     {
177         Product[] purchased = new Product[_purchased.size()];
178         for (int i=0;i<purchased.length;i++) {
179             purchased[i] = (Product)_products.get(_purchased.get(i));
180         }
181
182         return purchased;
183     }
184 }
185
Popular Tags