Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extPublishGeoserver / src / org / gvsig / publish / geoserver / conf / GSConfiguration.java @ 22616

History | View | Annotate | Download (6.46 KB)

1
/* gvSIG. Sistema de Información Geográfica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004-2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ibañez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
package org.gvsig.publish.geoserver.conf;
43
import java.io.BufferedReader;
44
import java.io.File;
45
import java.io.IOException;
46
import java.io.InputStream;
47
import java.io.InputStreamReader;
48
import java.net.URL;
49
import java.net.URLConnection;
50
import java.util.HashMap;
51
import java.util.Iterator;
52
import java.util.Map;
53

    
54
import org.gvsig.publish.PublishLogger;
55
import org.gvsig.publish.geoserver.model.Geoserver;
56

    
57
/**
58
 * This class represents the Geoserver Configuration.
59
 * It has the responsibility to generate the Geoserver XML configuration files (servers.xml, catalog.xml, info.xml) 
60
 * If the configuration exists it will be removed
61
 * @author jvhigon (josevicente.higon@iver.es)
62
 */
63
public class GSConfiguration  {
64
        private Geoserver model = null;
65
        //Attributes from associations
66
        private GSServer serverConf = null; 
67
        private Map featureTypes = null;
68
        private GSCatalog catalog;
69
                
70

    
71
        /**
72
         * Default constructor
73
         */
74
        public GSConfiguration (Geoserver geoserver){
75
                this.model = geoserver;
76
                this.serverConf = new GSServer(geoserver);
77
                this.featureTypes = new HashMap();
78
                this.catalog = new GSCatalog();
79
        }
80

    
81
        /**
82
         * 
83
         * @return object that represents Geoserver's services
84
         */
85
        public GSServer getGlobals(){
86
                return this.serverConf;
87
        }
88

    
89
        /**
90
         * Reloads Geoserver in order to apply changes in the configuration 
91
         * @param serverurl For example http://localhost:8080/geoserver
92
         * @throws IOException 
93
         */
94
        public void reload(URL serverurl, String user, String password) throws IOException {
95

    
96
                String aux = serverurl.toString() + "/admin/loginSubmit.do?username="+user+"&password="+password+"&submit=Submit";
97
                URL url = new URL(aux);
98
                URLConnection conn = url.openConnection();
99
                InputStream inStream = conn.getInputStream();
100
                String responseString=new String();
101
                BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
102
                while(in.ready()){   
103
                        responseString+= in.readLine();  
104
                }                                                
105
                // reload
106
                String cookie=conn.getHeaderField("Set-Cookie");
107

    
108
                cookie = cookie.substring(0, cookie.indexOf(";"));
109
                String cookieName = cookie.substring(0, cookie.indexOf("="));
110
                String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
111
                String cookieString=cookieName+"="+cookieValue;
112
                URL url2=new URL(serverurl.toString()+"/admin/loadFromXML.do");
113
                URLConnection conn2 = url2.openConnection();
114
                conn2.setRequestProperty("Cookie",cookieString);                 // set the Cookie for request
115
                conn2.connect();
116
                inStream = conn2.getInputStream();
117
                in = new BufferedReader(new InputStreamReader(inStream));
118
                responseString=new String();
119
                while(in.ready()){  
120
                        responseString+= in.readLine();  
121
                }                                                
122
                //logout
123
                URL url3=new URL(serverurl.toString()+"/admin/logout.do");
124
                URLConnection conn3 = url3.openConnection();
125
                conn3.setRequestProperty("Cookie",cookieString);
126
                conn3.connect();
127
                inStream = conn3.getInputStream();
128
                in = new BufferedReader(new InputStreamReader(inStream));
129
                responseString=new String();
130
                while(in.ready()){   
131
                        responseString+= in.readLine(); 
132
                }
133
        }
134

    
135

    
136
        /**
137
         * Adds a new feature for publishing.
138
         * Adds a datastore, namespace and style into the catalog 
139
         * 
140
         * @param feature
141
         * @return false if the datasoruce already exists
142
         */
143
        public boolean addFeatureType(GSFeature feature){
144
                if (featureTypes.containsKey(feature.getId())==true){
145
                        PublishLogger.getLog().error("ERROR " + getClass().getName() + ": This feature already exists");
146
                }
147
                featureTypes.put(feature.getId(), feature);
148
                boolean alreadyExists = catalog.addDatastore(feature.getDatastore());                
149
                catalog.addNamespace(feature.getNamespace());
150
                catalog.addStyle(feature.getStyle());
151
                if (alreadyExists){
152
                        return false;
153
                }else{
154
                        return true;
155
                }
156
        }
157

    
158
        /**
159
         * Writes the Geoserver config files
160
         * 
161
         */
162
        public void toXML(){        
163
                File dir = model.getConfigDirectory();
164
                //generates catalog.xml
165
                File catalog_xml = new File(dir.getAbsolutePath() + File.separator +"catalog.xml");
166
                this.catalog.toXML(catalog_xml);
167
                //generates services.xml
168
                File services_xml = new File(dir.getAbsolutePath() + File.separator +"services.xml");
169
                this.serverConf.toXML(services_xml);        
170
                //clean directories        
171
                File feature_dir = new File(dir.getAbsoluteFile()+ File.separator + "featureTypes");
172
                File styles_dir = new File(dir.getAbsolutePath() + File.separator + "styles");
173
                if (feature_dir.exists()){                
174
                        removeDirectory(feature_dir);                
175
                }
176
                feature_dir.mkdirs();
177
                if (styles_dir.exists()){
178
                        removeDirectory(styles_dir);        
179
                }
180
                styles_dir.mkdir();
181
                //generates info.xml (and write sld into directory styles)
182
                Iterator i = featureTypes.values().iterator();
183
                while (i.hasNext()){
184
                        GSFeature feature = (GSFeature)i.next();                
185
                        feature.toXML(feature_dir);                                   
186
                }             
187
                //creates directory coverages & plugIns
188
                File coverages_dir = new File(dir.getAbsoluteFile()+ File.separator + "coverages");
189
                if (!coverages_dir.exists()) coverages_dir.mkdirs();
190
                File plugins_dir = new File(dir.getAbsoluteFile()+ File.separator + "plugIns");
191
                if (!plugins_dir.exists()) plugins_dir.mkdirs();
192
        }
193
        /**
194
         * Delete all directory contents
195
         * @param dir
196
         */
197
        private void removeDirectory(File dir){
198
                File[] files = dir.listFiles();
199
                if (files != null){
200
                        for (int i = 0; i < files.length; i++){
201
                                removeDirectory(files[i]);
202
                        }            
203
                }    
204
                dir.delete();
205
        }
206

    
207
}