Source Code Cross Referenced for AdminContext.java in  » Web-Server » Jigsaw » org » w3c » jigsaw » admin » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » Jigsaw » org.w3c.jigsaw.admin 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // AdminContext.java
002:        // $Id: AdminContext.java,v 1.10 2003/02/28 10:33:03 ylafon Exp $
003:        // (c) COPYRIGHT MIT and INRIA, 1996.
004:        // Please first read the full copyright statement in file COPYRIGHT.html
005:
006:        package org.w3c.jigsaw.admin;
007:
008:        import java.io.BufferedInputStream;
009:        import java.io.IOException;
010:        import java.io.InputStream;
011:        import java.io.PrintStream;
012:
013:        import java.util.Hashtable;
014:
015:        import java.util.zip.GZIPInputStream;
016:
017:        import java.net.URL;
018:
019:        import org.w3c.www.mime.MimeType;
020:
021:        import org.w3c.www.http.HTTP;
022:        import org.w3c.www.http.HttpCredential;
023:        import org.w3c.www.http.HttpEntityMessage;
024:        import org.w3c.www.http.HttpMessage;
025:        import org.w3c.www.http.HttpReplyMessage;
026:        import org.w3c.www.http.HttpRequestMessage;
027:
028:        import org.w3c.www.protocol.http.HttpManager;
029:        import org.w3c.www.protocol.http.Reply;
030:        import org.w3c.www.protocol.http.Request;
031:
032:        /**
033:         * The client side Admin context.
034:         */
035:
036:        public class AdminContext {
037:            public static final boolean debug = false;
038:
039:            public static MimeType conftype = null;
040:            static {
041:                try {
042:                    conftype = new MimeType(
043:                            "application/xml;type=jigsaw-config;charset=UTF-8");
044:                } catch (Exception ex) {
045:                    ex.printStackTrace();
046:                    System.exit(1);
047:                }
048:            }
049:
050:            /**
051:             * Cached attribute descriptions:
052:             */
053:            protected Hashtable cachedattrs = new Hashtable();
054:            /**
055:             * The root URL for the target admin serv(l)er
056:             */
057:            protected URL server = null;
058:            /**
059:             * The RemoteRoot resource for all administered servers.
060:             */
061:            protected RemoteResource root = null;
062:            /**
063:             * The HTTP manager used to access the remote admin server.
064:             */
065:            protected HttpManager http = null;
066:            /**
067:             * The Admin protocol decoder.
068:             */
069:            protected AdminReader reader = null;
070:            /**
071:             * The Admin protocol encoder.
072:             */
073:            protected AdminWriter writer = null;
074:            /**
075:             * The credential used for authentification
076:             */
077:            protected HttpCredential credential = null;
078:
079:            protected InputStream getInputStream(Reply reply)
080:                    throws IOException {
081:                if (reply.hasTransferEncoding("gzip"))
082:                    return new GZIPInputStream(reply.getInputStream());
083:                else
084:                    return reply.getInputStream();
085:            }
086:
087:            protected String getContent(Reply reply) {
088:                try {
089:                    InputStream is = getInputStream(reply);
090:                    if (is == null)
091:                        return null;
092:                    BufferedInputStream bis = new BufferedInputStream(is);
093:                    byte b[] = new byte[256];
094:                    StringBuffer buffer = new StringBuffer();
095:                    while ((bis.read(b, 0, 256)) != -1)
096:                        buffer.append(new String(b));
097:                    bis.close();
098:                    return new String(buffer);
099:                } catch (IOException ex) {
100:                    return null;
101:                }
102:            }
103:
104:            /**
105:             * Run the given (tunneling) HTTP request.
106:             * This method will check that the appropriate MIME types are used.
107:             * @param request The request ti run.
108:             * @return A Reply instance.
109:             * @exception RemoteAccessException If some network error occurs.
110:             */
111:
112:            protected Reply runRequest(Request request)
113:                    throws RemoteAccessException {
114:                if (credential != null) {
115:                    request.setAuthorization(credential);
116:                }
117:                try {
118:                    Reply reply = http.runRequest(request);
119:                    MimeType type = reply.getContentType();
120:                    if (reply.getStatus() == HTTP.UNAUTHORIZED) {
121:                        getInputStream(reply).close();
122:                        throw new RemoteAccessException("Unauthorized");
123:                    }
124:                    if ((type == null) || (type.match(conftype) < 0)) {
125:                        String content = getContent(reply);
126:                        if (content != null)
127:                            throw new RemoteAccessException(content);
128:                        throw new RemoteAccessException("invalid content type");
129:                    }
130:                    return reply;
131:                } catch (RemoteAccessException ex) {
132:                    throw ex;
133:                } catch (Exception ex) {
134:                    throw new RemoteAccessException(ex.getMessage());
135:                }
136:            }
137:
138:            /**
139:             * Query the admin server for its main root resource.
140:             * @exception RemoteAccessException If some network error occurs.
141:             */
142:
143:            protected synchronized void loadRoot() throws RemoteAccessException {
144:                try {
145:                    // Prepare the HTTP request:
146:                    Request req = http.createRequest();
147:                    req.setMethod("LOAD-ROOT");
148:                    req.setURL(server);
149:                    req.setValue("TE", "gzip");
150:                    // Run that request:
151:                    Reply rep = runRequest(req);
152:                    // Decode the reply:
153:                    root = reader.readResource(server, null,
154:                            getInputStream(rep));
155:                } catch (RemoteAccessException ex) {
156:                    if (debug)
157:                        ex.printStackTrace();
158:                    throw ex;
159:                } catch (Exception ex) {
160:                    root = null;
161:                    if (debug)
162:                        ex.printStackTrace();
163:                    throw new RemoteAccessException(ex.getMessage());
164:                }
165:            }
166:
167:            /**
168:             * Get the root admin for all servers managed by target admin server.
169:             * @return A RemoteResource instance.
170:             * @exception RemoteAccessException If target server is unreachable.
171:             */
172:
173:            public synchronized RemoteResource getAdminResource()
174:                    throws RemoteAccessException {
175:                if (root == null)
176:                    loadRoot();
177:                return root;
178:            }
179:
180:            /**
181:             * sets the credential to be used for authentification
182:             */
183:
184:            public void setCredential(HttpCredential cr) {
185:                credential = cr;
186:            }
187:
188:            /**
189:             * initialize the context
190:             * @exception RemoteAccessException if a remote access error occurs.
191:             */
192:            public void initialize() throws RemoteAccessException {
193:                loadRoot();
194:            }
195:
196:            /**
197:             * Connect to the given admin server.
198:             * @exception RemoteAccessException if a remote access error occurs.
199:             */
200:
201:            public AdminContext(URL server) throws RemoteAccessException {
202:                this .server = server;
203:                this .http = HttpManager.getManager();
204:                this .reader = new AdminReader(this );
205:                this .writer = new AdminWriter();
206:            }
207:
208:            public static void main(String args[]) {
209:                try {
210:                    AdminContext adm = new AdminContext(new URL(args[0]));
211:                    adm.initialize();
212:                    String cmd = args[1];
213:                    // Get the root resource:
214:                    PlainRemoteResource root = (PlainRemoteResource) adm.root;
215:                    // Lookup some child:
216:                    RemoteResource child = root;
217:                    for (int i = 2; i < args.length; i++)
218:                        child = child.loadResource(args[i]);
219:                    // Perform a command:
220:                    if (cmd.equals("dump")) {
221:                        ((PlainRemoteResource) child).dump(System.out);
222:                    } else if (cmd.equals("list")) {
223:                        if (!child.isContainer()) {
224:                            System.out.println("\tnot a container !");
225:                        } else {
226:                            String e[] = child.enumerateResourceIdentifiers();
227:                            for (int i = 0; i < e.length; i++)
228:                                System.out.println("\t" + e[i]);
229:                        }
230:                    }
231:                } catch (Exception ex) {
232:                    ex.printStackTrace();
233:                }
234:            }
235:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.