KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > prevayler > demo > Application


1 package org.apache.ojb.broker.prevayler.demo;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17  
18 import org.apache.ojb.broker.PersistenceBroker;
19 import org.apache.ojb.broker.PersistenceBrokerFactory;
20 import org.apache.ojb.broker.util.ui.AsciiSplash;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.util.Vector JavaDoc;
25 /**
26  * The tutorial application.
27  * @author Thomas Mahler
28  */

29 public class Application
30 {
31     private Vector JavaDoc useCases;
32     private PersistenceBroker broker;
33     /**
34      * Application constructor comment.
35      */

36     public Application()
37     {
38         broker = null;
39         try
40         {
41             broker = PersistenceBrokerFactory.defaultPersistenceBroker();
42         }
43         catch (Throwable JavaDoc t)
44         {
45             t.printStackTrace();
46         }
47         useCases = new Vector JavaDoc();
48         useCases.add(new UCListAllProducts(broker));
49         useCases.add(new UCEnterNewProduct(broker));
50         useCases.add(new UCEditProduct(broker));
51         useCases.add(new UCDeleteProduct(broker));
52         useCases.add(new UCQuitApplication(broker));
53     }
54     /**
55      * Disply available use cases.
56      */

57     public void displayUseCases()
58     {
59         System.out.println();
60         for (int i = 0; i < useCases.size(); i++)
61         {
62             System.out.println("[" + i + "] " + ((UseCase) useCases.get(i)).getDescription());
63         }
64     }
65     /**
66      * Insert the method's description here.
67      * Creation date: (04.03.2001 10:40:25)
68      * @param args java.lang.String[]
69      */

70     public static void main(String JavaDoc[] args)
71     {
72         Application app = new Application();
73         app.run();
74     }
75     /**
76      * read a single line from stdin and return as String
77      */

78     private String JavaDoc readLine()
79     {
80         try
81         {
82             BufferedReader JavaDoc rin = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
83             return rin.readLine();
84         }
85         catch (Exception JavaDoc e)
86         {
87             return "";
88         }
89     }
90     /**
91      * the applications main loop.
92      */

93     public void run()
94     {
95         System.out.println(AsciiSplash.getSplashArt());
96         System.out.println("Welcome to the OJB PB tutorial application");
97         System.out.println();
98         // never stop (there is a special use case to quit the application)
99
while (true)
100         {
101             try
102             {
103                 // select a use case and perform it
104
UseCase uc = selectUseCase();
105                 uc.apply();
106             }
107             catch (Throwable JavaDoc t)
108             {
109                 broker.close();
110                 System.out.println(t.getMessage());
111             }
112         }
113     }
114     /**
115      * select a use case.
116      */

117     public UseCase selectUseCase()
118     {
119         displayUseCases();
120         System.out.println("type in number to select a use case");
121         String JavaDoc in = readLine();
122         int index = Integer.parseInt(in);
123         return (UseCase) useCases.get(index);
124     }
125     
126
127 }
128
Popular Tags