Source Code Cross Referenced for Serveur.java in  » Web-Server » javahttpserver » httpserver » 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 » javahttpserver » httpserver 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * <p>
003:         * Title: RequeteHTTP
004:         * </p>
005:         * 
006:         * <p>
007:         * Description: Represente le serveur JavaHttpServer
008:         * </p>
009:         * 
010:         * <p>
011:         * Copyright: Copyright (c) 2005
012:         * </p>
013:         * 
014:         * <p>
015:         * Company:
016:         * </p>
017:         * 
018:         * @author Adlani Anouar - Detante Antoine - Klein Gregory - Pepin Pierre
019:         * @version 1.0
020:         */package httpserver;
021:
022:        import java.net.*;
023:        import java.io.*;
024:
025:        public class Serveur {
026:
027:            // version du protocole
028:            public static String VERSION_PROTOCOLE = new String("HTTP/1.0");
029:            // nom du serveur
030:            public static String NOM_SERVEUR = new String("JavaHttpServer");
031:            // chemin où se trouve les pages web hebergees
032:            public static String root;
033:            // page par defaut à affiche
034:            public static String defaultPage;
035:            // port du serveur
036:            private int port;
037:            // type mime
038:            private Typesmime types;
039:            // socket d'ecoute
040:            private ServerSocket socketEcoute;
041:
042:            public static Config config;
043:
044:            /**
045:             * Créer une instance de Serveur
046:             * @param p port du serveur
047:             * @param r chemin où se trouve les pages web hebergees
048:             */
049:            public Serveur(int p, String r) {
050:                Serveur.root = r;
051:                port = p;
052:                defaultPage = new String("index.html");
053:                try {
054:                    Serveur.testWwwDirectory();
055:                    System.out.println("[info] repertoire www (" + Serveur.root
056:                            + ")... OK");
057:                } catch (Exception ex) {
058:                    System.err
059:                            .println("[erreur-fatale] impossible d'initialiser le repertoire www : "
060:                                    + ex.getMessage());
061:                    System.exit(1);
062:                }
063:                try {
064:                    types = new Typesmime();
065:                    System.out
066:                            .println("[info] chargement des types MIME... OK");
067:                } catch (FileNotFoundException ex) {
068:                    System.err
069:                            .println("[erreur-fatale] Un fichier de configuration manque : impossible de charger les types MIME\n"
070:                                    + ex.getMessage());
071:                    System.exit(1);
072:                } catch (IOException ex) {
073:                    System.err
074:                            .println("[erreur-fatale] Erreur lors de la lecture des types MIME\n"
075:                                    + ex.getMessage());
076:                    System.exit(1);
077:                }
078:            }
079:
080:            public Serveur(Config config) {
081:                Serveur.config = config;
082:                Serveur.root = config.getWWWDirectory();
083:                port = config.getPort();
084:                defaultPage = config.getDefaultPageName();
085:                try {
086:                    Serveur.testWwwDirectory();
087:                    System.out.println("[info] repertoire www (" + Serveur.root
088:                            + ")... OK");
089:                } catch (Exception ex) {
090:                    System.err
091:                            .println("[erreur-fatale] impossible d'initialiser le repertoire www : "
092:                                    + ex.getMessage());
093:                    System.exit(1);
094:                }
095:                try {
096:                    if (config.getTypesMimeFile() != null) {
097:                        types = new Typesmime(config.getTypesMimeFile());
098:                    } else
099:                        types = new Typesmime();
100:                    System.out.println("[info] chargement des types MIME ("
101:                            + types.getFileName() + ")... OK");
102:                } catch (FileNotFoundException ex) {
103:                    System.err
104:                            .println("[erreur-fatale] Un fichier de configuration manque : impossible de charger les types MIME depuis "
105:                                    + types.getFileName());
106:                    System.exit(1);
107:                } catch (IOException ex) {
108:                    System.err
109:                            .println("[erreur-fatale] Erreur lors de la lecture des types MIME depuis "
110:                                    + types.getFileName());
111:                    System.exit(1);
112:                }
113:            }
114:
115:            /**
116:             * mutli-connexion des clients
117:             */
118:            public void mainLoop() {
119:                System.out.println("[info] " + Serveur.NOM_SERVEUR
120:                        + " connecte sur le port " + this .port
121:                        + " - en attente de clients...\n");
122:                while (true) {
123:                    try {
124:                        Socket client = socketEcoute.accept();
125:                        System.out.println("[info] Connection du client "
126:                                + client.getInetAddress().getHostAddress());
127:                        Connection nouvelle = new Connection(client, types);
128:                        nouvelle.start();
129:                    } catch (IOException ex) {
130:                        System.err
131:                                .println("[erreur] Erreur lors de la connection d'un client");
132:                    }
133:                }
134:            }
135:
136:            /**
137:             * Ouverture des sockets du serveur
138:             */
139:            public void connecter() {
140:                try {
141:                    socketEcoute = new ServerSocket(this .port);
142:                } catch (IOException ioe) {
143:                    System.err
144:                            .println("[erreur] Impossible de connecter le serveur : \n"
145:                                    + ioe.getMessage());
146:                    System.exit(1);
147:                }
148:            }
149:
150:            /**
151:             * Permet de tester si le repertoire www est correct 
152:             * @throws Exception levee si : le repertoire n'existe pas, ce n'est pas un repertoire
153:             * ou les droits sur le repertoire empeche la lecture.
154:             */
155:            public static void testWwwDirectory() throws Exception {
156:                File wwwDir = new File(Serveur.root);
157:                if (!wwwDir.exists())
158:                    throw new Exception(Serveur.root + " n'existe pas");
159:                if (!wwwDir.isDirectory())
160:                    throw new Exception(Serveur.root
161:                            + " n'est pas un repertoire");
162:                if (!wwwDir.canRead())
163:                    throw new Exception("Impossibe de lire dans le repertoire "
164:                            + Serveur.root);
165:            }
166:
167:            /**
168:             * methode main qui lance le serveur
169:             * @param args liste des arguments
170:             */
171:            public static void main(String args[]) {
172:                Serveur serveur = null;
173:
174:                Config config = new Config();
175:                String configFilename = "config.xml";
176:                if (args.length > 0)
177:                    configFilename = args[0];
178:                else {
179:                    System.err.println("Usage: java " + Serveur.class.getName()
180:                            + " <fichier configuration>");
181:                    System.exit(1);
182:                }
183:                try {
184:                    config.getFromFile(configFilename);
185:                    System.out
186:                            .println("[info] Configuration chargee a partir du fichier "
187:                                    + configFilename);
188:                } catch (Exception ex) {
189:                    System.err
190:                            .println("[erreur-fatale] Impossible de charger la configuration du serveur : \n "
191:                                    + ex.getMessage());
192:                    System.err.println("Usage: java " + Serveur.class.getName()
193:                            + " <fichier configuration>");
194:
195:                    System.exit(1);
196:                }
197:                serveur = new Serveur(config);
198:                serveur.connecter();
199:                serveur.mainLoop();
200:            }
201:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.