~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to java/test/Util/Application.java

  • Committer: Bazaar Package Importer
  • Author(s): Cleto Martin Angelina
  • Date: 2011-04-25 18:44:24 UTC
  • mfrom: (6.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110425184424-sep9i9euu434vq4c
Tags: 3.4.1-7
* Bug fix: "libdb5.1-java.jar was renamed to db.jar", thanks to Ondřej
  Surý (Closes: #623555).
* Bug fix: "causes noise in php5", thanks to Jayen Ashar (Closes:
  #623533).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// **********************************************************************
 
2
//
 
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
 
4
//
 
5
// This copy of Ice is licensed to you under the terms described in the
 
6
// ICE_LICENSE file included in this distribution.
 
7
//
 
8
// **********************************************************************
 
9
 
 
10
package test.Util;
 
11
 
 
12
import Ice.Communicator;
 
13
import Ice.InitializationData;
 
14
import Ice.LocalException;
 
15
import Ice.LoggerI;
 
16
import Ice.StringSeqHolder;
 
17
import Ice.Util;
 
18
 
 
19
public abstract class Application
 
20
{
 
21
    public final int WAIT = 2;
 
22
 
 
23
    public interface ServerReadyListener
 
24
    {
 
25
        void serverReady();
 
26
    };
 
27
 
 
28
    public interface CommunicatorListener
 
29
    {
 
30
        void communicatorInitialized(Communicator c);
 
31
    };
 
32
 
 
33
    public
 
34
    Application()
 
35
    {
 
36
    }
 
37
 
 
38
    //
 
39
    // This main() must be called by the global main(). main()
 
40
    // initializes the Communicator, calls run(), and destroys
 
41
    // the Communicator upon return from run(). It thereby handles
 
42
    // all exceptions properly, i.e., error messages are printed
 
43
    // if exceptions propagate to main(), and the Communicator is
 
44
    // always destroyed, regardless of exceptions.
 
45
    //
 
46
    public final int
 
47
    main(String appName, String[] args)
 
48
    {
 
49
        Ice.StringSeqHolder argsH = new Ice.StringSeqHolder(args);
 
50
        Ice.InitializationData initData = getInitData(argsH);
 
51
        return main(appName, argsH.value, initData);
 
52
    }
 
53
 
 
54
    public final int
 
55
    main(String appName, String[] args, InitializationData initializationData)
 
56
    {
 
57
        java.io.PrintWriter writer = getWriter();
 
58
                if(_communicator != null)
 
59
        {
 
60
            writer.println(appName + ": only one instance of the Application class can be used");
 
61
            return 1;
 
62
        }
 
63
 
 
64
        _testName = appName;
 
65
 
 
66
        //
 
67
        // We parse the properties here to extract Ice.ProgramName.
 
68
        // 
 
69
        StringSeqHolder argHolder = new StringSeqHolder(args);
 
70
        if(initializationData == null)
 
71
        {
 
72
            initializationData = getInitData(argHolder);
 
73
        }
 
74
        
 
75
        InitializationData initData;
 
76
        if(initializationData != null)
 
77
        {
 
78
            initData = (InitializationData)initializationData.clone();
 
79
        }
 
80
        else
 
81
        {
 
82
            initData = new InitializationData();
 
83
        }
 
84
        initData.properties = Util.createProperties(argHolder, initData.properties);
 
85
 
 
86
        //
 
87
        // If the process logger is the default logger, we replace it with a
 
88
        // a logger which is using the program name for the prefix.
 
89
        //
 
90
        if(Util.getProcessLogger() instanceof LoggerI)
 
91
        {
 
92
            Util.setProcessLogger(new LoggerI(initData.properties.getProperty("Ice.ProgramName"), ""));
 
93
        }
 
94
 
 
95
        int status = 0;
 
96
 
 
97
        try
 
98
        {
 
99
            _communicator = Util.initialize(argHolder, initData);
 
100
            if(_communicatorListener != null)
 
101
            {
 
102
                _communicatorListener.communicatorInitialized(_communicator);
 
103
            }
 
104
            status = run(argHolder.value);
 
105
            if(status == WAIT)
 
106
            {
 
107
                if(_cb != null)
 
108
                {
 
109
                    _cb.serverReady();
 
110
                }
 
111
                _communicator.waitForShutdown();
 
112
                status = 0;
 
113
            }
 
114
        }
 
115
        catch(LocalException ex)
 
116
        {
 
117
            writer.println(_testName + ": " + ex);
 
118
            ex.printStackTrace();
 
119
            status = 1;
 
120
        }
 
121
        catch(java.lang.Exception ex)
 
122
        {
 
123
            writer.println(_testName + ": unknown exception");
 
124
            ex.printStackTrace(writer);
 
125
            status = 1;
 
126
        }
 
127
        catch(java.lang.Error err)
 
128
        {
 
129
            //
 
130
            // We catch Error to avoid hangs in some non-fatal situations
 
131
            //
 
132
            writer.println(_testName + ": Java error");
 
133
            err.printStackTrace(writer);
 
134
            status = 1;
 
135
        }
 
136
 
 
137
        if(_communicator != null)
 
138
        {
 
139
            try
 
140
            {
 
141
                _communicator.destroy();
 
142
            }
 
143
            catch(LocalException ex)
 
144
            {
 
145
                writer.println(_testName + ": " + ex);
 
146
                ex.printStackTrace(writer);
 
147
                status = 1;
 
148
            }
 
149
            catch(java.lang.Exception ex)
 
150
            {
 
151
                writer.println(_testName + ": unknown exception");
 
152
                ex.printStackTrace(writer);
 
153
                status = 1;
 
154
            }
 
155
            _communicator = null;
 
156
        }
 
157
        writer.flush();
 
158
 
 
159
        return status;
 
160
    }
 
161
 
 
162
    public void stop()
 
163
    {
 
164
        if(_communicator != null)
 
165
        {
 
166
            _communicator.shutdown();
 
167
        }
 
168
    }
 
169
 
 
170
    // Initialize a new communicator.
 
171
    public Ice.Communicator initialize(InitializationData initData)
 
172
    {
 
173
        Ice.Communicator communicator = Util.initialize(initData);
 
174
        if(_communicatorListener != null)
 
175
        {
 
176
            _communicatorListener.communicatorInitialized(communicator);
 
177
        }
 
178
        return communicator;
 
179
    }
 
180
 
 
181
    public abstract int run(String[] args);
 
182
 
 
183
    // Hook to override the initialization data. This hook is
 
184
    // necessary because some properties must be set prior to
 
185
    // communicator initialization.
 
186
    protected Ice.InitializationData getInitData(Ice.StringSeqHolder argsH)
 
187
    {
 
188
        return null;
 
189
    }
 
190
 
 
191
    public java.io.PrintWriter getWriter()
 
192
    {
 
193
        return _printWriter;
 
194
    }
 
195
 
 
196
    public void setWriter(java.io.Writer writer)
 
197
    {
 
198
        _printWriter = new java.io.PrintWriter(writer);
 
199
    }
 
200
 
 
201
    public void setServerReadyListener(ServerReadyListener cb)
 
202
    {
 
203
        _cb = cb;
 
204
    }
 
205
 
 
206
    public void setCommunicatorListener(CommunicatorListener listener)
 
207
    {
 
208
        _communicatorListener = listener;
 
209
    }
 
210
 
 
211
    public void serverReady()
 
212
    {
 
213
        if(_cb != null)
 
214
        {
 
215
            _cb.serverReady();
 
216
        }
 
217
    }
 
218
 
 
219
    //
 
220
    // Return the application name, i.e., argv[0].
 
221
    //
 
222
    public String
 
223
    appName()
 
224
    {
 
225
        return _testName;
 
226
    }
 
227
 
 
228
    public Communicator
 
229
    communicator()
 
230
    {
 
231
        return _communicator;
 
232
    }
 
233
 
 
234
    private String _testName;
 
235
    private Communicator _communicator;
 
236
    private java.io.PrintWriter _printWriter = new java.io.PrintWriter(new java.io.OutputStreamWriter(System.out));
 
237
    private ServerReadyListener _cb = null;
 
238
    private CommunicatorListener _communicatorListener = null;
 
239
}