View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.cloudmgr;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  
41  import javax.inject.Inject;
42  
43  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployEnvironment;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FtpInfo;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
47  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.IAction;
48  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.AbstractServerApplicationService;
49  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IActionService;
50  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IFastDeployEnvironmentService;
51  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
52  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
53  import fr.paris.lutece.portal.service.datastore.DatastoreService;
54  import fr.paris.lutece.portal.service.i18n.I18nService;
55  import fr.paris.lutece.portal.service.spring.SpringContextService;
56  import fr.paris.lutece.portal.service.util.AppLogService;
57  import fr.paris.lutece.portal.service.util.AppPropertiesService;
58  import org.apache.commons.lang.StringUtils;
59  
60  public class ServerApplicationService extends AbstractServerApplicationService
61  {
62   
63  
64    
65      
66  
67      public List<ServerApplicationInstance> getListServerApplicationInstanceByEnvironment(FastDeployApplicationData data, String strIdApplication, String strCodeEnvironment,
68              String strServerApplicationType, Locale locale, boolean withActions, boolean withStatus )
69      {
70          String strPlateformEnvironmentBaseUrl = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_ENVIRONMENT_PLATEFORM_BASE_URL );
71          List<ServerApplicationInstance> listServerApplicationInstance = new ArrayList<ServerApplicationInstance>( );
72          List<String> listStrServerApplicationInstance;
73          String strWebserviceServerApplicationJsonObjectName = AppPropertiesService
74                  .getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_SERVER_APPLICATION_INSTANCES_JSON_OBJECT_NAME );
75          String strWebserviceServerApplicationJsonDictionaryName = AppPropertiesService
76                  .getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_SERVER_APPLICATION_INSTANCES_JSON_DICTIONARY_NAME );
77          String strJSONServerApplicationInstances = null;
78  
79          try
80          {
81              strJSONServerApplicationInstances = CloudmgrUtils.callPlateformEnvironmentWs( strPlateformEnvironmentBaseUrl
82                      + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
83                      + CloudmgrUtils.getPlateformUrlServerApplicationInstances( data.getApplicationCode( ), strCodeEnvironment, strServerApplicationType ) );
84          }
85          catch( Exception e )
86          {
87              AppLogService.debug( e );
88          }
89  
90          if ( strJSONServerApplicationInstances != null )
91          {
92              listStrServerApplicationInstance = DeploymentUtils.getJSONDictionary( strWebserviceServerApplicationJsonObjectName,
93                      strWebserviceServerApplicationJsonDictionaryName, strJSONServerApplicationInstances );
94  
95              for ( String strServerApplicationInstance : listStrServerApplicationInstance )
96              {
97                  listServerApplicationInstance.add( getServerApplicationInstance( data, strIdApplication, strServerApplicationInstance.toLowerCase( ), strCodeEnvironment,
98                          strServerApplicationType, locale, withActions, withStatus ) );
99              }
100         }
101 
102         return listServerApplicationInstance;
103     }
104 
105     public ServerApplicationInstance getServerApplicationInstance( FastDeployApplicationData data, String strIdApplication, String strServerInstanceCode, String strCodeEnvironment,
106             String strServerType, Locale locale, boolean withActions, boolean withStatus )
107     {
108        
109 
110         ServerApplicationInstance defaultServerApplicationInstance = _hashServerApplicationInstance.get( strServerType
111                 + ConstanteUtils.CONSTANTE_SEPARATOR_POINT + strServerInstanceCode );
112         ServerApplicationInstanceutece/plugins/appcenter/modules/fastdeployapplication/business/ServerApplicationInstance.html#ServerApplicationInstance">ServerApplicationInstance serverApplicationInstance =(ServerApplicationInstance)defaultServerApplicationInstance.clone();
113         serverApplicationInstance.setCodeEnvironment( strCodeEnvironment );
114 
115         String strServerNameSave = getServerNameSaved( strIdApplication, strCodeEnvironment, strServerType, strServerInstanceCode );
116         if ( !StringUtils.isEmpty( strServerNameSave ) )
117         {
118         	serverApplicationInstance.setName( strServerNameSave );
119         }
120         setFtpInfo( serverApplicationInstance,data.getApplicationCode( ) );
121 
122         if ( withActions )
123         {
124             serverApplicationInstance.setListServerApplicationAction( _actionService.getListActionByServerApplicationInstance( data,
125                     serverApplicationInstance, locale ) );
126         }
127 
128         if ( withStatus )
129         {
130             serverApplicationInstance.setStatus( getStatus( data, serverApplicationInstance ) );
131         }
132 
133         return serverApplicationInstance;
134     }
135 
136     private void setFtpInfo( ServerApplicationInstance serverApplicationInstance, String strCodeApplication )
137     {
138         serverApplicationInstance.setFtpDirectoryInstall(CloudmgrUtils.getDirectoryInstall(strCodeApplication,serverApplicationInstance));
139         serverApplicationInstance.setFtpDirectoryScript(CloudmgrUtils.getDirectoryScript(strCodeApplication,serverApplicationInstance));
140         serverApplicationInstance.setFtpDirectoryDump(CloudmgrUtils.getDirectoryDump(strCodeApplication,serverApplicationInstance));
141         serverApplicationInstance.setFtpDirectoryContext(CloudmgrUtils.getContextDirectoryTarget( strCodeApplication, serverApplicationInstance ));
142         
143         
144         
145         FtpInfo/plugins/appcenter/modules/fastdeployapplication/business/FtpInfo.html#FtpInfo">FtpInfo ftpInfo = new FtpInfo( );
146         ftpInfo.setHost( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_HOST ) );
147         ftpInfo.setPort( AppPropertiesService.getPropertyInt( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_PORT, 21 ) );
148         ftpInfo.setUserLogin( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_USER_LOGIN ) );
149         ftpInfo.setUserPassword( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_USER_PASSWORD ) );
150         
151         serverApplicationInstance.setFtpInfo( ftpInfo );
152     }
153 
154     
155 
156     
157     private Integer getStatus( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance )
158     {
159         if ( ( serverApplicationInstance != null ) && ( serverApplicationInstance.getListServerApplicationAction( ) != null ) )
160         {
161             for ( IAction action : serverApplicationInstance.getListServerApplicationAction( ) )
162             {
163                 if ( action.isUsedForStatus( ) )
164                 {
165                     boolean bResultOk = _actionService.executeAction( data, action, serverApplicationInstance, null );
166 
167                     return new Integer( new Boolean( bResultOk ) ? 1 : 0 );
168                 }
169             }
170         }
171 
172         return null;
173     }
174 
175     public void saveServerName( String strServerName, String strIdApplication, String strCodeEnvironment, String strServerType, String strCodeServerApplicationInstance )
176     {
177         DatastoreService.setDataValue( strIdApplication + "_" + strCodeEnvironment + "_" + strServerType + "_" + strCodeServerApplicationInstance + "_" + SERVER_NAME, strServerName );
178     }
179 
180     public String getServerNameSaved( String strIdApplication, String strCodeEnvironment, String strServerType, String strCodeServerApplicationInstance )
181     {
182         return DatastoreService.getDataValue( strIdApplication + "_" + strCodeEnvironment + "_" + strServerType + "_" + strCodeServerApplicationInstance + "_" + SERVER_NAME, null );
183     }
184 }