~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to tools/SwingStoreBrowser/src/com/novell/simias/storebrowser/.svn/text-base/SwingStoreBrowser.java.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***********************************************************************
2
 
 *  $RCSfile$
3
 
 * 
4
 
 *  Copyright (C) 2004 Novell, Inc.
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or
7
 
 *  modify it under the terms of the GNU General Public
8
 
 *  License as published by the Free Software Foundation; either
9
 
 *  version 2 of the License, or (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 *  Library General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
 *
20
 
 *  Author: Boyd Timothy <btimothy@novell.com>
21
 
 * 
22
 
 ***********************************************************************/
23
 
 
24
 
package com.novell.simias.storebrowser;
25
 
 
26
 
import java.net.MalformedURLException;
27
 
import java.text.MessageFormat;
28
 
import java.net.URL;
29
 
import java.io.*;
30
 
 
31
 
import com.novell.simias.browser.Browser_x0020_ServiceLocator;
32
 
import com.novell.simias.browser.Browser_x0020_ServiceSoap;
33
 
 
34
 
/**
35
 
 * @author Boyd Timothy <boyd@timothy.ws>
36
 
 */
37
 
public class SwingStoreBrowser {
38
 
 
39
 
        public SwingStoreBrowser()
40
 
        {
41
 
                
42
 
        }
43
 
 
44
 
        public static void main(String[] args) {
45
 
                StoreBrowserWindow sbWin = new StoreBrowserWindow();
46
 
                sbWin.setSize(800,600);
47
 
                //sbWin.pack();
48
 
                sbWin.setVisible(true);
49
 
        }
50
 
 
51
 
        public static Browser_x0020_ServiceSoap getSoapService(String serviceURL, String username, String password) throws MalformedURLException
52
 
        {
53
 
                String protocol;
54
 
 
55
 
            Browser_x0020_ServiceLocator locator =
56
 
                new Browser_x0020_ServiceLocator();
57
 
 
58
 
            URL url = new URL(serviceURL);
59
 
 
60
 
            Browser_x0020_ServiceSoap service = null;
61
 
        try
62
 
                {
63
 
                // make sure that the axis client knows to use a session
64
 
                locator.setMaintainSession(true);
65
 
 
66
 
                service = locator.getBrowser_x0020_ServiceSoap(url);
67
 
 
68
 
                        if (username != null)
69
 
                                ((org.apache.axis.client.Stub) service).setUsername(username);
70
 
                        if (password != null)
71
 
                                ((org.apache.axis.client.Stub) service).setPassword(password);
72
 
                        
73
 
            // if we make it here without exceptions, we've found a valid server
74
 
            return service;
75
 
                }
76
 
        catch (Exception e)
77
 
                {
78
 
            e.printStackTrace();
79
 
                }
80
 
        
81
 
        return null;
82
 
        }
83
 
        
84
 
        public static Browser_x0020_ServiceSoap getLocalSoapService()
85
 
        {
86
 
                String username = null;
87
 
                String password = null;
88
 
                
89
 
            URL url = getLocalServiceURL();
90
 
                if (url == null)
91
 
                        return null;
92
 
 
93
 
                SimiasCredentials credentials = SimiasCredentials.getLocalSimiasCredentials();
94
 
                if (credentials != null)
95
 
                {
96
 
                        username = credentials.getUsername();
97
 
                        password = credentials.getPassword();
98
 
                }
99
 
                
100
 
                try
101
 
                {
102
 
                        return getSoapService(url.toString(), username, password);
103
 
                }
104
 
                catch (MalformedURLException e)
105
 
                {
106
 
                        // Intentionally left blank
107
 
                }
108
 
                
109
 
                return null;
110
 
        }
111
 
        
112
 
        private static URL getLocalServiceURL()
113
 
        {
114
 
                String userHomePath = System.getProperty("user.home");
115
 
                String fileSeparator = System.getProperty("file.separator");
116
 
                
117
 
                if (userHomePath == null || fileSeparator == null)
118
 
                        return null;
119
 
                
120
 
                String configFilePath =
121
 
                        MessageFormat.format("{0}{1}.local{1}share{1}simias{1}Simias.config",
122
 
                                                                 new Object[] {userHomePath, fileSeparator});
123
 
 
124
 
                try
125
 
                {
126
 
                        BufferedReader br = new BufferedReader(new FileReader(configFilePath));
127
 
                        
128
 
                        String line = br.readLine();
129
 
                        while (line != null)
130
 
                        {
131
 
                                int settingPos = line.indexOf("<setting name=\"WebServiceUri\"");
132
 
                                if (settingPos >= 0)
133
 
                                {
134
 
                                        // Attempt to parse out the URL
135
 
                                        int valuePos = line.indexOf("value=\"", settingPos + 1);
136
 
                                        if (valuePos >= 0)
137
 
                                        {
138
 
                                                int closingQuotePos = line.indexOf("\"", valuePos + 7);
139
 
                                                if (closingQuotePos >= 0)
140
 
                                                {
141
 
                                                        String urlString = line.substring(valuePos + 7, closingQuotePos);
142
 
                                                        try
143
 
                                                        {
144
 
                                                                return new URL(urlString + fileSeparator + "SimiasBrowser.asmx");
145
 
                                                        }
146
 
                                                        catch (MalformedURLException e)
147
 
                                                        {
148
 
                                                                System.err.println("Couldn't parse WebServiceUri from Simias.config");
149
 
                                                                return null;
150
 
                                                        }
151
 
                                                }
152
 
                                        }
153
 
                                }
154
 
                                
155
 
                                line = br.readLine();
156
 
                        }
157
 
                        
158
 
                        br.close();
159
 
                }
160
 
                catch (IOException ioe)
161
 
                {
162
 
                        System.err.println("Couldn't open: " + configFilePath);
163
 
                        ioe.printStackTrace();
164
 
                        return null;
165
 
                }
166
 
                catch(Exception e)
167
 
                {
168
 
                        System.err.println("Parse error: " + configFilePath);
169
 
                        e.printStackTrace();
170
 
                }
171
 
 
172
 
                return null;
173
 
        }
174
 
 
175
 
        private static class SimiasCredentials
176
 
        {
177
 
                String username;
178
 
                String password;
179
 
                
180
 
                public SimiasCredentials(String username, String password)
181
 
                {
182
 
                        this.username = username;
183
 
                        this.password = password;
184
 
                }
185
 
                
186
 
                public String getUsername()
187
 
                {
188
 
                        return username;
189
 
                }
190
 
                
191
 
                public String getPassword()
192
 
                {
193
 
                        return password;
194
 
                }
195
 
                
196
 
                public static SimiasCredentials getLocalSimiasCredentials()
197
 
                {
198
 
                        // Read the Simias Credentials from the disk and return
199
 
                        // a SimiasCredentials Object
200
 
                        String username = System.getProperty("user.name");
201
 
                        String userHomePath = System.getProperty("user.home");
202
 
                        String fileSeparator = System.getProperty("file.separator");
203
 
                        String password = null;
204
 
                        
205
 
                        if (username == null || userHomePath == null || fileSeparator == null)
206
 
                                return null;
207
 
 
208
 
                        String passwordFilePath =
209
 
                                MessageFormat.format("{0}{1}.local{1}share{1}simias{1}.local.if",
210
 
                                                                         new Object[] {userHomePath, fileSeparator});
211
 
 
212
 
                        try
213
 
                        {
214
 
                                BufferedReader br = new BufferedReader(new FileReader(passwordFilePath));
215
 
                                
216
 
                                password = br.readLine();
217
 
                                
218
 
                                br.close();
219
 
                        }
220
 
                        catch (Exception e)
221
 
                        {
222
 
                                return null;
223
 
                        }
224
 
 
225
 
                        return new SimiasCredentials(username, password);
226
 
                }
227
 
        }
228
 
}