http://git-wip-us.apache.org/repos/asf/airavata/blob/b4ede9cb/modules/gfac/gfac-impl/src/test/java/org/apache/airavata/core/gfac/services/impl/GSISSHProviderTestWithMyProxyAuth.java ---------------------------------------------------------------------- diff --cc modules/gfac/gfac-impl/src/test/java/org/apache/airavata/core/gfac/services/impl/GSISSHProviderTestWithMyProxyAuth.java index 0000000,9e03173..61db417 mode 000000,100644..100644 --- a/modules/gfac/gfac-impl/src/test/java/org/apache/airavata/core/gfac/services/impl/GSISSHProviderTestWithMyProxyAuth.java +++ b/modules/gfac/gfac-impl/src/test/java/org/apache/airavata/core/gfac/services/impl/GSISSHProviderTestWithMyProxyAuth.java @@@ -1,0 -1,229 +1,229 @@@ + ///* + // * + // * Licensed to the Apache Software Foundation (ASF) under one + // * or more contributor license agreements. See the NOTICE file + // * distributed with this work for additional information + // * regarding copyright ownership. The ASF licenses this file + // * to you under the Apache License, Version 2.0 (the + // * "License"); you may not use this file except in compliance + // * with the License. You may obtain a copy of the License at + // * + // * http://www.apache.org/licenses/LICENSE-2.0 + // * + // * Unless required by applicable law or agreed to in writing, + // * software distributed under the License is distributed on an + // * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + // * KIND, either express or implied. See the License for the + // * specific language governing permissions and limitations + // * under the License. + // * + //*/ + //package org.apache.airavata.core.gfac.services.impl; + // + //import java.io.File; + //import java.net.URL; + //import java.util.ArrayList; + //import java.util.Date; + //import java.util.List; + //import java.util.UUID; + // + //import org.apache.aiaravata.application.catalog.data.model.ApplicationInterface; + //import org.apache.airavata.commons.gfac.type.ActualParameter; + //import org.apache.airavata.commons.gfac.type.ApplicationDescription; + //import org.apache.airavata.commons.gfac.type.HostDescription; + //import org.apache.airavata.commons.gfac.type.ServiceDescription; + //import org.apache.airavata.gfac.GFacConfiguration; + //import org.apache.airavata.gfac.GFacException; + //import org.apache.airavata.gfac.SecurityContext; + //import org.apache.airavata.gfac.core.context.ApplicationContext; + //import org.apache.airavata.gfac.core.context.JobExecutionContext; + //import org.apache.airavata.gfac.core.context.MessageContext; + //import org.apache.airavata.gfac.impl.BetterGfacImpl; + //import org.apache.airavata.gfac.gsissh.security.GSISecurityContext; + //import org.apache.airavata.gfac.ssh.api.Cluster; + //import org.apache.airavata.gfac.ssh.api.SSHApiException; + //import org.apache.airavata.gfac.ssh.api.ServerInfo; + //import GSIAuthenticationInfo; + //import org.apache.airavata.gfac.ssh.impl.PBSCluster; + //import org.apache.airavata.gfac.ssh.impl.authentication.MyProxyAuthenticationInfo; + //import org.apache.airavata.gfac.ssh.util.CommonUtils; + //import org.apache.airavata.model.appcatalog.appdeployment.ApplicationDeploymentDescription; + //import org.apache.airavata.model.workspace.experiment.TaskDetails; -//import org.apache.airavata.persistance.registry.jpa.impl.RegistryFactory; ++//import org.apache.airavata.experiment.registry.jpa.impl.RegistryFactory; + //import org.apache.airavata.schemas.gfac.ApplicationDeploymentDescriptionType; + //import org.apache.airavata.schemas.gfac.GsisshHostType; + //import org.apache.airavata.schemas.gfac.HpcApplicationDeploymentType; + //import org.apache.airavata.schemas.gfac.InputParameterType; + //import org.apache.airavata.schemas.gfac.JobTypeType; + //import org.apache.airavata.schemas.gfac.OutputParameterType; + //import org.apache.airavata.schemas.gfac.ProjectAccountType; + //import org.apache.airavata.schemas.gfac.QueueType; + //import org.apache.airavata.schemas.gfac.StringParameterType; + //import org.testng.annotations.BeforeClass; + //import org.testng.annotations.Test; + // + //public class GSISSHProviderTestWithMyProxyAuth { + // private JobExecutionContext jobExecutionContext; + // + // //FIXME: move job properties to configuration file + // private static final String hostAddress = "trestles.sdsc.edu"; + // private static final String hostName = "trestles"; + // private String myProxyUserName; + // private String myProxyPassword; + // private String workingDirectory; + // private String certificateLocation = "/Users/lahirugunathilake/Downloads/certificates"; + // + // @BeforeClass + // public void setUp() throws Exception { + //// System.setProperty("myproxy.user", "ogce"); + //// System.setProperty("myproxy.password", ""); + //// System.setProperty("basedir", "/Users/lahirugunathilake/Downloads"); + //// System.setProperty("gsi.working.directory", "/home/ogce"); + //// System.setProperty("gsi.certificate.path", "/Users/lahirugunathilake/Downloads/certificates"); + // certificateLocation = System.getProperty("trusted.cert.location"); + // myProxyUserName = System.getProperty("myproxy.username"); + // myProxyPassword = System.getProperty("myproxy.password"); + // workingDirectory = System.getProperty("gsi.working.directory"); + // + // if (myProxyUserName == null || myProxyPassword == null || certificateLocation == null) { + // System.out.println(">>>>>> Please run tests with my proxy user name and password. " + + // "E.g :- mvn clean install -Dmyproxy.username=xxx -Dmyproxy.password=xxx -Dgsi.working.directory=/path<<<<<<<"); + // throw new Exception("Need my proxy user name password to run tests."); + // } + // URL resource = GSISSHProviderTestWithMyProxyAuth.class.getClassLoader().getResource(org.apache.airavata.common.utils.Constants.GFAC_CONFIG_XML); + // assert resource != null; + // System.out.println(resource.getFile()); + // GFacConfiguration gFacConfiguration = GFacConfiguration.create(new File(resource.getPath()), null); + // + // /* + // * Host + // */ + // HostDescription host = new HostDescription(GsisshHostType.type); + // host.getType().setHostAddress(hostAddress); + // host.getType().setHostName(hostName); + // + // /* + // * App + // */ + // ApplicationDescription appDesc = new ApplicationDescription(HpcApplicationDeploymentType.type); + // HpcApplicationDeploymentType app = (HpcApplicationDeploymentType) appDesc.getType(); + // ApplicationDeploymentDescriptionType.ApplicationName name = ApplicationDeploymentDescriptionType.ApplicationName.Factory.newInstance(); + // name.setStringValue("EchoLocal"); + // app.setApplicationName(name); + // ProjectAccountType projectAccountType = app.addNewProjectAccount(); + // projectAccountType.setProjectAccountNumber("sds128"); + // + // QueueType queueType = app.addNewQueue(); + // queueType.setQueueName("normal"); + // + // app.setCpuCount(1); + // app.setJobType(JobTypeType.SERIAL); + // app.setNodeCount(1); + // app.setProcessorsPerNode(1); + // + // /* + // * Use bat file if it is compiled on Windows + // */ + // app.setExecutableLocation("/bin/echo"); + // + // /* + // * Default tmp location + // */ + // String tempDir = "/home/ogce/scratch/"; + // String date = (new Date()).toString(); + // date = date.replaceAll(" ", "_"); + // date = date.replaceAll(":", "_"); + // + // tempDir = workingDirectory + File.separator + // + "SimpleEcho" + "_" + date + "_" + UUID.randomUUID(); + // + // System.out.println(tempDir); + // app.setScratchWorkingDirectory(tempDir); + // app.setStaticWorkingDirectory(tempDir); + // app.setInputDataDirectory(tempDir + File.separator + "inputData"); + // app.setOutputDataDirectory(tempDir + File.separator + "outputData"); + // app.setStandardOutput(tempDir + File.separator + app.getApplicationName().getStringValue() + ".stdout"); + // app.setStandardError(tempDir + File.separator + app.getApplicationName().getStringValue() + ".stderr"); + // app.setMaxWallTime(5); + // app.setInstalledParentPath("/opt/torque/bin/"); + // + // /* + // * Service + // */ + // ServiceDescription serv = new ServiceDescription(); + // serv.getType().setName("SimpleEcho"); + // + // List<InputParameterType> inputList = new ArrayList<InputParameterType>(); + // + // InputParameterType input = InputParameterType.Factory.newInstance(); + // input.setParameterName("echo_input"); + // input.setParameterType(StringParameterType.Factory.newInstance()); + // inputList.add(input); + // + // InputParameterType[] inputParamList = inputList.toArray(new InputParameterType[inputList + // + // .size()]); + // List<OutputParameterType> outputList = new ArrayList<OutputParameterType>(); + // OutputParameterType output = OutputParameterType.Factory.newInstance(); + // output.setParameterName("echo_output"); + // output.setParameterType(StringParameterType.Factory.newInstance()); + // outputList.add(output); + // + // OutputParameterType[] outputParamList = outputList + // .toArray(new OutputParameterType[outputList.size()]); + // + // serv.getType().setInputParametersArray(inputParamList); + // serv.getType().setOutputParametersArray(outputParamList); + // + // jobExecutionContext = new JobExecutionContext(gFacConfiguration, serv.getType().getName()); + // // Adding security context + // jobExecutionContext.addSecurityContext(GSISecurityContext.GSI_SECURITY_CONTEXT, getSecurityContext(app)); + // ApplicationContext applicationContext = new ApplicationContext(); + // jobExecutionContext.setApplicationContext(applicationContext); + // applicationContext.setServiceDescription(serv); + // applicationContext.setApplicationDeploymentDescription(appDesc); + // applicationContext.setHostDescription(host); + // + // MessageContext inMessage = new MessageContext(); + // ActualParameter echo_input = new ActualParameter(); + // ((StringParameterType) echo_input.getType()).setValue("echo_output=hello"); + // inMessage.addParameter("echo_input", echo_input); + // + // + // jobExecutionContext.setInMessageContext(inMessage); + // + // MessageContext outMessage = new MessageContext(); + // ActualParameter echo_out = new ActualParameter(); + //// ((StringParameterType)echo_input.getType()).setValue("echo_output=hello"); + // outMessage.addParameter("echo_output", echo_out); + // jobExecutionContext.setRegistry(RegistryFactory.getLoggingRegistry()); + // jobExecutionContext.setTaskData(new TaskDetails("11323")); + // jobExecutionContext.setOutMessageContext(outMessage); + // + // } + // + // private SecurityContext getSecurityContext(HpcApplicationDeploymentType app) { + // GSIAuthenticationInfo authenticationInfo + // = new MyProxyAuthenticationInfo(myProxyUserName, myProxyPassword, "myproxy.teragrid.org", + // 7512, 17280000, certificateLocation); + // + // // Server info + // ServerInfo serverInfo = new ServerInfo("ogce", "trestles.sdsc.edu"); + // Cluster pbsCluster = null; + // try { + // pbsCluster = new PBSCluster(serverInfo, authenticationInfo, CommonUtils.getPBSJobManager(app.getInstalledParentPath())); + // } catch (SSHApiException e) { + // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + // } + // GSISecurityContext sshSecurityContext = new GSISecurityContext(pbsCluster); + // return sshSecurityContext; + // } + // @Test + // public void testGSISSHProvider() throws GFacException { + // BetterGfacImpl gFacAPI = new BetterGfacImpl(); + // gFacAPI.submitJob(jobExecutionContext.getExperimentID(), jobExecutionContext.getTaskData().getTaskID(), jobExecutionContext.getGatewayID()); + // System.out.println(jobExecutionContext.getJobDetails().getJobDescription()); + // System.out.println(jobExecutionContext.getJobDetails().getJobID()); + // } + // + //}
http://git-wip-us.apache.org/repos/asf/airavata/blob/b4ede9cb/modules/gfac/gfac-impl/src/test/java/org/apache/airavata/core/gfac/services/impl/LocalProviderTest.java ---------------------------------------------------------------------- diff --cc modules/gfac/gfac-impl/src/test/java/org/apache/airavata/core/gfac/services/impl/LocalProviderTest.java index 0000000,aeb8158..b3974d2 mode 000000,100644..100644 --- a/modules/gfac/gfac-impl/src/test/java/org/apache/airavata/core/gfac/services/impl/LocalProviderTest.java +++ b/modules/gfac/gfac-impl/src/test/java/org/apache/airavata/core/gfac/services/impl/LocalProviderTest.java @@@ -1,0 -1,184 +1,184 @@@ + ///* + // * + // * Licensed to the Apache Software Foundation (ASF) under one + // * or more contributor license agreements. See the NOTICE file + // * distributed with this work for additional information + // * regarding copyright ownership. The ASF licenses this file + // * to you under the Apache License, Version 2.0 (the + // * "License"); you may not use this file except in compliance + // * with the License. You may obtain a copy of the License at + // * + // * http://www.apache.org/licenses/LICENSE-2.0 + // * + // * Unless required by applicable law or agreed to in writing, + // * software distributed under the License is distributed on an + // * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + // * KIND, either express or implied. See the License for the + // * specific language governing permissions and limitations + // * under the License. + // * + //*/ + //package org.apache.airavata.core.gfac.services.impl; + // + //import java.io.File; + //import java.net.URL; + //import java.util.ArrayList; + //import java.util.List; + // + //import org.apache.airavata.common.utils.MonitorPublisher; + //import org.apache.airavata.commons.gfac.type.ActualParameter; + //import org.apache.airavata.commons.gfac.type.ApplicationDescription; + //import org.apache.airavata.commons.gfac.type.HostDescription; + //import org.apache.airavata.commons.gfac.type.ServiceDescription; + //import org.apache.airavata.gfac.GFacConfiguration; + //import org.apache.airavata.gfac.GFacException; + //import org.apache.airavata.gfac.core.context.ApplicationContext; + //import org.apache.airavata.gfac.core.context.JobExecutionContext; + //import org.apache.airavata.gfac.core.context.MessageContext; + //import org.apache.airavata.gfac.core.provider.GFacProviderException; + //import org.apache.airavata.gfac.local.handler.LocalDirectorySetupHandler; + //import org.apache.airavata.gfac.local.provider.impl.LocalProvider; + //import org.apache.airavata.model.workspace.experiment.ExecutionUnit; + //import org.apache.airavata.model.workspace.experiment.Experiment; + //import org.apache.airavata.model.workspace.experiment.TaskDetails; + //import org.apache.airavata.model.workspace.experiment.WorkflowNodeDetails; -//import org.apache.airavata.persistance.registry.jpa.impl.LoggingRegistryImpl; ++//import org.apache.airavata.experiment.registry.jpa.impl.LoggingRegistryImpl; + //import org.apache.airavata.schemas.gfac.ApplicationDeploymentDescriptionType; + //import org.apache.airavata.schemas.gfac.InputParameterType; + //import org.apache.airavata.schemas.gfac.OutputParameterType; + //import org.apache.airavata.schemas.gfac.StringParameterType; + //import org.apache.commons.lang.SystemUtils; + //import org.testng.annotations.BeforeTest; + //import org.testng.annotations.Test; + // + //import com.google.common.eventbus.EventBus; + // + //public class LocalProviderTest { + // private JobExecutionContext jobExecutionContext; + // @BeforeTest + // public void setUp() throws Exception { + // + // URL resource = this.getClass().getClassLoader().getResource(org.apache.airavata.common.utils.Constants.GFAC_CONFIG_XML); + // File configFile = new File(resource.getPath()); + // GFacConfiguration gFacConfiguration = GFacConfiguration.create(configFile, null); + // //have to set InFlwo Handlers and outFlowHandlers + // ApplicationContext applicationContext = new ApplicationContext(); + // HostDescription host = new HostDescription(); + // host.getType().setHostName("localhost"); + // host.getType().setHostAddress("localhost"); + // applicationContext.setHostDescription(host); + // /* + // * App + // */ + // ApplicationDescription appDesc = new ApplicationDescription(); + // ApplicationDeploymentDescriptionType app = appDesc.getType(); + // ApplicationDeploymentDescriptionType.ApplicationName name = ApplicationDeploymentDescriptionType.ApplicationName.Factory.newInstance(); + // name.setStringValue("EchoLocal"); + // app.setApplicationName(name); + // + // /* + // * Use bat file if it is compiled on Windows + // */ + // if (SystemUtils.IS_OS_WINDOWS) { + // URL url = this.getClass().getClassLoader().getResource("echo.bat"); + // app.setExecutableLocation(url.getFile()); + // } else { + // //for unix and Mac + // app.setExecutableLocation("/bin/echo"); + // } + // + // /* + // * Default tmp location + // */ + // String tempDir = System.getProperty("java.io.tmpdir"); + // if (tempDir == null) { + // tempDir = "/tmp"; + // } + // + // app.setScratchWorkingDirectory(tempDir); + // app.setStaticWorkingDirectory(tempDir); + // app.setInputDataDirectory(tempDir + File.separator + "input"); + // app.setOutputDataDirectory(tempDir + File.separator + "output"); + // app.setStandardOutput(tempDir + File.separator + "echo.stdout"); + // app.setStandardError(tempDir + File.separator + "echo.stderr"); + // + // applicationContext.setApplicationDeploymentDescription(appDesc); + // + // /* + // * Service + // */ + // ServiceDescription serv = new ServiceDescription(); + // serv.getType().setName("SimpleEcho"); + // + // List<InputParameterType> inputList = new ArrayList<InputParameterType>(); + // InputParameterType input = InputParameterType.Factory.newInstance(); + // input.setParameterName("echo_input"); + // input.setParameterType(StringParameterType.Factory.newInstance()); + // inputList.add(input); + // InputParameterType[] inputParamList = inputList.toArray(new InputParameterType[inputList + // .size()]); + // + // List<OutputParameterType> outputList = new ArrayList<OutputParameterType>(); + // OutputParameterType output = OutputParameterType.Factory.newInstance(); + // output.setParameterName("echo_output"); + // output.setParameterType(StringParameterType.Factory.newInstance()); + // outputList.add(output); + // OutputParameterType[] outputParamList = outputList + // .toArray(new OutputParameterType[outputList.size()]); + // + // serv.getType().setInputParametersArray(inputParamList); + // serv.getType().setOutputParametersArray(outputParamList); + // + // jobExecutionContext = new JobExecutionContext(gFacConfiguration, serv.getType().getName()); + // jobExecutionContext.setApplicationContext(applicationContext); + // /* + // * Host + // */ + // applicationContext.setServiceDescription(serv); + // + // MessageContext inMessage = new MessageContext(); + // ActualParameter echo_input = new ActualParameter(); + // ((StringParameterType) echo_input.getType()).setValue("echo_output=hello"); + // inMessage.addParameter("echo_input", echo_input); + // + // jobExecutionContext.setInMessageContext(inMessage); + // + // MessageContext outMessage = new MessageContext(); + // ActualParameter echo_out = new ActualParameter(); + // outMessage.addParameter("echo_output", echo_out); + // + // jobExecutionContext.setOutMessageContext(outMessage); + // + // jobExecutionContext.setExperimentID("test123"); + // jobExecutionContext.setExperiment(new Experiment("test123","project1","admin","testExp")); + // jobExecutionContext.setTaskData(new TaskDetails(jobExecutionContext.getExperimentID())); + // jobExecutionContext.setRegistry(new LoggingRegistryImpl()); + // jobExecutionContext.setWorkflowNodeDetails(new WorkflowNodeDetails(jobExecutionContext.getExperimentID(),"none", ExecutionUnit.APPLICATION)); + // + // + // } + // + // @Test + // public void testLocalDirectorySetupHandler() throws GFacException { + // LocalDirectorySetupHandler localDirectorySetupHandler = new LocalDirectorySetupHandler(); + // localDirectorySetupHandler.invoke(jobExecutionContext); + // + // ApplicationDescription applicationDeploymentDescription = jobExecutionContext.getApplicationContext().getApplicationDeploymentDescription(); + // ApplicationDeploymentDescriptionType app = applicationDeploymentDescription.getType(); + // junit.framework.Assert.assertTrue(new File(app.getStaticWorkingDirectory()).exists()); + // junit.framework.Assert.assertTrue(new File(app.getScratchWorkingDirectory()).exists()); + // junit.framework.Assert.assertTrue(new File(app.getInputDataDirectory()).exists()); + // junit.framework.Assert.assertTrue(new File(app.getOutputDataDirectory()).exists()); + // } + // + // @Test + // public void testLocalProvider() throws GFacException,GFacProviderException { + // LocalDirectorySetupHandler localDirectorySetupHandler = new LocalDirectorySetupHandler(); + // localDirectorySetupHandler.invoke(jobExecutionContext); + // LocalProvider localProvider = new LocalProvider(); + // localProvider.setMonitorPublisher(new MonitorPublisher(new EventBus())); + // localProvider.initialize(jobExecutionContext); + // localProvider.execute(jobExecutionContext); + // localProvider.dispose(jobExecutionContext); + // } + //} http://git-wip-us.apache.org/repos/asf/airavata/blob/b4ede9cb/modules/gfac/gfac-service/pom.xml ---------------------------------------------------------------------- diff --cc modules/gfac/gfac-service/pom.xml index 0000000,99497a3..83999e3 mode 000000,100644..100644 --- a/modules/gfac/gfac-service/pom.xml +++ b/modules/gfac/gfac-service/pom.xml @@@ -1,0 -1,100 +1,100 @@@ + <?xml version="1.0" encoding="UTF-8"?> + + <!--Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file + distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under + the Apache License, Version 2.0 (theà "License"); you may not use this file except in compliance with the License. You may + obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to + in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF + ANY ~ KIND, either express or implied. See the License for the specific language governing permissions and limitations under + the License. --> + + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <artifactId>airavata-gfac</artifactId> + <groupId>org.apache.airavata</groupId> + <version>0.16-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <name>Airavata Gfac Service</name> + <artifactId>airavata-gfac-service</artifactId> + <packaging>jar</packaging> + <url>http://airavata.apache.org/</url> + + <dependencies> + <dependency> + <groupId>org.apache.thrift</groupId> + <artifactId>libthrift</artifactId> + <version>${thrift.version}</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>${org.slf4j.version}</version> + </dependency> + <!--<dependency>--> + <!--<groupId>org.apache.airavata</groupId>--> + <!--<artifactId>airavata-client-api</artifactId>--> + <!--<version>${project.version}</version>--> + <!--</dependency>--> + <dependency> + <groupId>org.apache.airavata</groupId> + <artifactId>airavata-common-utils</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.airavata</groupId> + <artifactId>airavata-registry-cpi</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.airavata</groupId> - <artifactId>airavata-jpa-registry</artifactId> ++ <artifactId>airavata-experiment-catalog</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.airavata</groupId> + <artifactId>airavata-gfac-client</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.airavata</groupId> + <artifactId>airavata-gfac-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.airavata</groupId> + <artifactId>airavata-gfac-impl</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.airavata</groupId> + <artifactId>airavata-model-utils</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.airavata</groupId> + <artifactId>airavata-api-stubs</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.airavata</groupId> + <artifactId>airavata-server-configuration</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.curator</groupId> + <artifactId>curator-framework</artifactId> + <version>${curator.version}</version> + </dependency> + </dependencies> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + </properties> + + </project> http://git-wip-us.apache.org/repos/asf/airavata/blob/b4ede9cb/modules/gfac/gfac-service/src/main/java/org/apache/airavata/gfac/server/GfacServerHandler.java ---------------------------------------------------------------------- diff --cc modules/gfac/gfac-service/src/main/java/org/apache/airavata/gfac/server/GfacServerHandler.java index 0000000,64c06e4..f6338a8 mode 000000,100644..100644 --- a/modules/gfac/gfac-service/src/main/java/org/apache/airavata/gfac/server/GfacServerHandler.java +++ b/modules/gfac/gfac-service/src/main/java/org/apache/airavata/gfac/server/GfacServerHandler.java @@@ -1,0 -1,421 +1,421 @@@ + /* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + package org.apache.airavata.gfac.server; + + import com.google.common.eventbus.EventBus; + import org.airavata.appcatalog.cpi.AppCatalog; + import org.apache.aiaravata.application.catalog.data.impl.AppCatalogFactory; + import org.apache.airavata.common.exception.AiravataException; + import org.apache.airavata.common.exception.ApplicationSettingsException; + import org.apache.airavata.common.logger.AiravataLogger; + import org.apache.airavata.common.logger.AiravataLoggerFactory; + import org.apache.airavata.common.utils.AiravataZKUtils; + import org.apache.airavata.common.utils.Constants; + import org.apache.airavata.common.utils.MonitorPublisher; + import org.apache.airavata.common.utils.ServerSettings; + import org.apache.airavata.common.utils.ThriftUtils; + import org.apache.airavata.common.utils.listener.AbstractActivityListener; + import org.apache.airavata.gfac.GFacConfiguration; + import org.apache.airavata.gfac.GFacException; + import org.apache.airavata.gfac.core.GFac; + import org.apache.airavata.gfac.core.handler.GFacHandlerConfig; + import org.apache.airavata.gfac.core.handler.GFacHandlerException; + import org.apache.airavata.gfac.core.handler.ThreadedHandler; + import org.apache.airavata.gfac.core.GFacThreadPoolExecutor; + import org.apache.airavata.gfac.core.GFacUtils; + import org.apache.airavata.gfac.cpi.GfacService; + import org.apache.airavata.gfac.cpi.gfac_cpi_serviceConstants; + import org.apache.airavata.gfac.impl.BetterGfacImpl; + import org.apache.airavata.gfac.impl.InputHandlerWorker; + import org.apache.airavata.messaging.core.MessageContext; + import org.apache.airavata.messaging.core.MessageHandler; + import org.apache.airavata.messaging.core.MessagingConstants; + import org.apache.airavata.messaging.core.Publisher; + import org.apache.airavata.messaging.core.PublisherFactory; + import org.apache.airavata.messaging.core.impl.RabbitMQTaskLaunchConsumer; + import org.apache.airavata.model.messaging.event.MessageType; + import org.apache.airavata.model.messaging.event.TaskSubmitEvent; + import org.apache.airavata.model.messaging.event.TaskTerminateEvent; + import org.apache.airavata.model.workspace.experiment.ExperimentState; + import org.apache.airavata.model.workspace.experiment.ExperimentStatus; -import org.apache.airavata.persistance.registry.jpa.impl.RegistryFactory; ++import org.apache.airavata.experiment.catalog.impl.RegistryFactory; + import org.apache.airavata.registry.cpi.Registry; + import org.apache.airavata.registry.cpi.RegistryException; + import org.apache.airavata.registry.cpi.RegistryModelType; + import org.apache.curator.RetryPolicy; + import org.apache.curator.framework.CuratorFramework; + import org.apache.curator.framework.CuratorFrameworkFactory; + import org.apache.curator.retry.ExponentialBackoffRetry; + import org.apache.thrift.TBase; + import org.apache.thrift.TException; + import org.apache.zookeeper.CreateMode; + import org.apache.zookeeper.ZooDefs; + import org.apache.zookeeper.data.Stat; + import org.xml.sax.SAXException; + + import javax.xml.parsers.ParserConfigurationException; + import javax.xml.xpath.XPathExpressionException; + import java.io.File; + import java.io.IOException; + import java.net.URL; + import java.util.ArrayList; + import java.util.Calendar; + import java.util.HashMap; + import java.util.List; + import java.util.Map; + import java.util.concurrent.BlockingQueue; + + public class GfacServerHandler implements GfacService.Iface { + private final static AiravataLogger logger = AiravataLoggerFactory.getLogger(GfacServerHandler.class); + private static RabbitMQTaskLaunchConsumer rabbitMQTaskLaunchConsumer; + private static int requestCount=0; + private Registry registry; + private AppCatalog appCatalog; + private String gatewayName; + private String airavataUserName; + private CuratorFramework curatorClient; + private MonitorPublisher publisher; + private String gfacServer; + private String gfacExperiments; + private String airavataServerHostPort; + private BlockingQueue<TaskSubmitEvent> taskSubmitEvents; + private static File gfacConfigFile; + private static List<ThreadedHandler> daemonHandlers = new ArrayList<ThreadedHandler>(); + private static List<AbstractActivityListener> activityListeners = new ArrayList<AbstractActivityListener>(); + + public GfacServerHandler() throws Exception { + try { + // start curator client + String zkhostPort = AiravataZKUtils.getZKhostPort(); + RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5); + curatorClient = CuratorFrameworkFactory.newClient(zkhostPort, retryPolicy); + curatorClient.start(); + gfacServer = ServerSettings.getSetting(Constants.ZOOKEEPER_GFAC_SERVER_NODE, "/gfac-server"); + gfacExperiments = ServerSettings.getSetting(Constants.ZOOKEEPER_GFAC_EXPERIMENT_NODE, "/gfac-experiments"); + airavataServerHostPort = ServerSettings.getSetting(Constants.GFAC_SERVER_HOST) + + ":" + ServerSettings.getSetting(Constants.GFAC_SERVER_PORT); + storeServerConfig(); + publisher = new MonitorPublisher(new EventBus()); + registry = RegistryFactory.getDefaultRegistry(); + appCatalog = AppCatalogFactory.getAppCatalog(); + setGatewayProperties(); + startDaemonHandlers(); + // initializing Better Gfac Instance + BetterGfacImpl.getInstance().init(registry, appCatalog, curatorClient, publisher); + if (ServerSettings.isGFacPassiveMode()) { + rabbitMQTaskLaunchConsumer = new RabbitMQTaskLaunchConsumer(); + rabbitMQTaskLaunchConsumer.listen(new TaskLaunchMessageHandler()); + } + startStatusUpdators(registry, curatorClient, publisher, rabbitMQTaskLaunchConsumer); + + } catch (Exception e) { + throw new Exception("Error initialising GFAC", e); + } + } + + public static void main(String[] args) { + RabbitMQTaskLaunchConsumer rabbitMQTaskLaunchConsumer = null; + try { + rabbitMQTaskLaunchConsumer = new RabbitMQTaskLaunchConsumer(); + rabbitMQTaskLaunchConsumer.listen(new TestHandler()); + } catch (AiravataException e) { + logger.error(e.getMessage(), e); + } + } + private void storeServerConfig() throws Exception { + Stat stat = curatorClient.checkExists().forPath(gfacServer); + if (stat == null) { + curatorClient.create().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE) + .forPath(gfacServer, new byte[0]); + } + String instanceId = ServerSettings.getSetting(Constants.ZOOKEEPER_GFAC_SERVER_NAME); + String instanceNode = gfacServer + File.separator + instanceId; + stat = curatorClient.checkExists().forPath(instanceNode); + if (stat == null) { + curatorClient.create().withMode(CreateMode.EPHEMERAL).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(instanceNode, airavataServerHostPort.getBytes()); + curatorClient.getChildren().watched().forPath(instanceNode); + } + stat = curatorClient.checkExists().forPath(gfacExperiments); + if (stat == null) { + curatorClient.create().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(gfacExperiments, airavataServerHostPort.getBytes()); + } + stat = curatorClient.checkExists().forPath(gfacExperiments + File.separator + instanceId); + if (stat == null) { + curatorClient.create().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE) + .forPath(gfacExperiments + File.separator + instanceId, airavataServerHostPort.getBytes()); + } + } + + private long ByateArrayToLong(byte[] data) { + long value = 0; + for (int i = 0; i < data.length; i++) + { + value += ((long) data[i] & 0xffL) << (8 * i); + } + return value; + } + + public String getGFACServiceVersion() throws TException { + return gfac_cpi_serviceConstants.GFAC_CPI_VERSION; + } + + /** + * * After creating the experiment Data and Task Data in the orchestrator + * * Orchestrator has to invoke this operation for each Task per experiment to run + * * the actual Job related actions. + * * + * * @param experimentID + * * @param taskID + * * @param gatewayId: + * * The GatewayId is inferred from security context and passed onto gfac. + * * @return sucess/failure + * * + * * + * + * @param experimentId + * @param taskId + * @param gatewayId + */ + public boolean submitJob(String experimentId, String taskId, String gatewayId, String tokenId) throws TException { + requestCount++; + logger.info("-----------------------------------------------------" + requestCount + "-----------------------------------------------------"); + logger.infoId(experimentId, "GFac Received submit job request for the Experiment: {} TaskId: {}", experimentId, taskId); + InputHandlerWorker inputHandlerWorker = new InputHandlerWorker(BetterGfacImpl.getInstance(), experimentId, + taskId, gatewayId, tokenId); + // try { + // if( gfac.submitJob(experimentId, taskId, gatewayId)){ + logger.debugId(experimentId, "Submitted job to the Gfac Implementation, experiment {}, task {}, gateway " + + "{}", experimentId, taskId, gatewayId); + + GFacThreadPoolExecutor.getCachedThreadPool().execute(inputHandlerWorker); + + // we immediately return when we have a threadpool + return true; + } + + public boolean cancelJob(String experimentId, String taskId, String gatewayId, String tokenId) throws TException { + logger.infoId(experimentId, "GFac Received cancel job request for Experiment: {} TaskId: {} ", experimentId, taskId); + try { + if (BetterGfacImpl.getInstance().cancel(experimentId, taskId, gatewayId, tokenId)) { + logger.debugId(experimentId, "Successfully cancelled job, experiment {} , task {}", experimentId, taskId); + return true; + } else { + logger.errorId(experimentId, "Job cancellation failed, experiment {} , task {}", experimentId, taskId); + return false; + } + } catch (Exception e) { + logger.errorId(experimentId, "Error cancelling the experiment {}.", experimentId); + throw new TException("Error cancelling the experiment : " + e.getMessage(), e); + } + } + + public Registry getRegistry() { + return registry; + } + + public void setRegistry(Registry registry) { + this.registry = registry; + } + + public String getGatewayName() { + return gatewayName; + } + + public void setGatewayName(String gatewayName) { + this.gatewayName = gatewayName; + } + + public String getAiravataUserName() { + return airavataUserName; + } + + public void setAiravataUserName(String airavataUserName) { + this.airavataUserName = airavataUserName; + } + + protected void setGatewayProperties() throws ApplicationSettingsException { + setAiravataUserName(ServerSettings.getDefaultUser()); + setGatewayName(ServerSettings.getDefaultUserGateway()); + } + + private GFac getGfac() throws TException { + GFac gFac = BetterGfacImpl.getInstance(); + gFac.init(registry, appCatalog, curatorClient, publisher); + return gFac; + } + + public void startDaemonHandlers() { + List<GFacHandlerConfig> daemonHandlerConfig = null; + String className = null; + try { + URL resource = GfacServerHandler.class.getClassLoader().getResource(org.apache.airavata.common.utils.Constants.GFAC_CONFIG_XML); + if (resource != null) { + gfacConfigFile = new File(resource.getPath()); + } + daemonHandlerConfig = GFacConfiguration.getDaemonHandlers(gfacConfigFile); + for (GFacHandlerConfig handlerConfig : daemonHandlerConfig) { + className = handlerConfig.getClassName(); + Class<?> aClass = Class.forName(className).asSubclass(ThreadedHandler.class); + ThreadedHandler threadedHandler = (ThreadedHandler) aClass.newInstance(); + threadedHandler.initProperties(handlerConfig.getProperties()); + daemonHandlers.add(threadedHandler); + } + } catch (ParserConfigurationException | IOException | XPathExpressionException | ClassNotFoundException | + InstantiationException | IllegalAccessException | GFacHandlerException | SAXException e) { + logger.error("Error parsing gfac-config.xml, double check the xml configuration", e); + } + for (ThreadedHandler tHandler : daemonHandlers) { + (new Thread(tHandler)).start(); + } + } + + + public static void startStatusUpdators(Registry registry, CuratorFramework curatorClient, MonitorPublisher publisher, + + RabbitMQTaskLaunchConsumer rabbitMQTaskLaunchConsumer) { + try { + String[] listenerClassList = ServerSettings.getActivityListeners(); + Publisher rabbitMQPublisher = PublisherFactory.createActivityPublisher(); + for (String listenerClass : listenerClassList) { + Class<? extends AbstractActivityListener> aClass = Class.forName(listenerClass).asSubclass(AbstractActivityListener.class); + AbstractActivityListener abstractActivityListener = aClass.newInstance(); + activityListeners.add(abstractActivityListener); + abstractActivityListener.setup(publisher, registry, curatorClient, rabbitMQPublisher, rabbitMQTaskLaunchConsumer); + logger.info("Registering listener: " + listenerClass); + publisher.registerListener(abstractActivityListener); + } + } catch (Exception e) { + logger.error("Error loading the listener classes configured in airavata-server.properties", e); + } + } + private static class TestHandler implements MessageHandler{ + @Override + public Map<String, Object> getProperties() { + Map<String, Object> props = new HashMap<String, Object>(); + ArrayList<String> keys = new ArrayList<String>(); + keys.add(ServerSettings.getLaunchQueueName()); + keys.add(ServerSettings.getCancelQueueName()); + props.put(MessagingConstants.RABBIT_ROUTING_KEY, keys); + props.put(MessagingConstants.RABBIT_QUEUE, ServerSettings.getLaunchQueueName()); + return props; + } + + @Override + public void onMessage(MessageContext message) { + TaskSubmitEvent event = new TaskSubmitEvent(); + TBase messageEvent = message.getEvent(); + byte[] bytes = new byte[0]; + try { + bytes = ThriftUtils.serializeThriftObject(messageEvent); + ThriftUtils.createThriftFromBytes(bytes, event); + System.out.println(event.getExperimentId()); + } catch (TException e) { + logger.error(e.getMessage(), e); + } + } + } + + private class TaskLaunchMessageHandler implements MessageHandler { + private String experimentNode; + private String nodeName; + + public TaskLaunchMessageHandler() { + experimentNode = ServerSettings.getSetting(Constants.ZOOKEEPER_GFAC_EXPERIMENT_NODE, "/gfac-experiments"); + nodeName = ServerSettings.getSetting(Constants.ZOOKEEPER_GFAC_SERVER_NAME,"gfac-node0"); + } + + public Map<String, Object> getProperties() { + Map<String, Object> props = new HashMap<String, Object>(); + ArrayList<String> keys = new ArrayList<String>(); + keys.add(ServerSettings.getLaunchQueueName()); + keys.add(ServerSettings.getCancelQueueName()); + props.put(MessagingConstants.RABBIT_ROUTING_KEY, keys); + props.put(MessagingConstants.RABBIT_QUEUE, ServerSettings.getLaunchQueueName()); + return props; + } + + public void onMessage(MessageContext message) { + System.out.println(" Message Received with message id '" + message.getMessageId() + + "' and with message type '" + message.getType()); + if (message.getType().equals(MessageType.LAUNCHTASK)) { + try { + TaskSubmitEvent event = new TaskSubmitEvent(); + TBase messageEvent = message.getEvent(); + byte[] bytes = ThriftUtils.serializeThriftObject(messageEvent); + ThriftUtils.createThriftFromBytes(bytes, event); + // update experiment status to executing + ExperimentStatus status = new ExperimentStatus(); + status.setExperimentState(ExperimentState.EXECUTING); + status.setTimeOfStateChange(Calendar.getInstance().getTimeInMillis()); + registry.update(RegistryModelType.EXPERIMENT_STATUS, status, event.getExperimentId()); + experimentNode = ServerSettings.getSetting(Constants.ZOOKEEPER_GFAC_EXPERIMENT_NODE, "/gfac-experiments"); + try { + GFacUtils.createExperimentEntryForPassive(event.getExperimentId(), event.getTaskId(), curatorClient, + experimentNode, nodeName, event.getTokenId(), message.getDeliveryTag()); + AiravataZKUtils.getExpStatePath(event.getExperimentId()); + submitJob(event.getExperimentId(), event.getTaskId(), event.getGatewayId(), event.getTokenId()); + } catch (Exception e) { + logger.error(e.getMessage(), e); + rabbitMQTaskLaunchConsumer.sendAck(message.getDeliveryTag()); + } + } catch (TException e) { + logger.error(e.getMessage(), e); //nobody is listening so nothing to throw + } catch (RegistryException e) { + logger.error("Error while updating experiment status", e); + } + } else if (message.getType().equals(MessageType.TERMINATETASK)) { + boolean cancelSuccess = false; + TaskTerminateEvent event = new TaskTerminateEvent(); + TBase messageEvent = message.getEvent(); + try { + byte[] bytes = ThriftUtils.serializeThriftObject(messageEvent); + ThriftUtils.createThriftFromBytes(bytes, event); + boolean saveDeliveryTagSuccess = GFacUtils.setExperimentCancel(event.getExperimentId(), curatorClient, message.getDeliveryTag()); + if (saveDeliveryTagSuccess) { + cancelSuccess = cancelJob(event.getExperimentId(), event.getTaskId(), event.getGatewayId(), event.getTokenId()); + System.out.println(" Message Received with message id '" + message.getMessageId() + + "' and with message type '" + message.getType()); + } else { + throw new GFacException("Terminate Task fail to save delivery tag : " + String.valueOf(message.getDeliveryTag()) + " \n" + + "This happens when another cancel operation is being processed or experiment is in one of final states, complete|failed|cancelled."); + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + }finally { + if (cancelSuccess) { + // if cancel success , AiravataExperimentStatusUpdator will send an ack to this message. + } else { + try { + if (GFacUtils.ackCancelRequest(event.getExperimentId(), curatorClient)) { + if (!rabbitMQTaskLaunchConsumer.isOpen()) { + rabbitMQTaskLaunchConsumer.reconnect(); + } + rabbitMQTaskLaunchConsumer.sendAck(message.getDeliveryTag()); + } + } catch (Exception e) { + logger.error("Error while ack to cancel request, experimentId: " + event.getExperimentId()); + } + } + } + } + } + } + } http://git-wip-us.apache.org/repos/asf/airavata/blob/b4ede9cb/modules/gfac/gfac-service/src/test/java/org/apache/airavata/gfac/client/util/Initialize.java ---------------------------------------------------------------------- diff --cc modules/gfac/gfac-service/src/test/java/org/apache/airavata/gfac/client/util/Initialize.java index 0000000,651f414..15d384c mode 000000,100644..100644 --- a/modules/gfac/gfac-service/src/test/java/org/apache/airavata/gfac/client/util/Initialize.java +++ b/modules/gfac/gfac-service/src/test/java/org/apache/airavata/gfac/client/util/Initialize.java @@@ -1,0 -1,330 +1,329 @@@ + /* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + + package org.apache.airavata.gfac.client.util; + + import org.apache.airavata.common.exception.ApplicationSettingsException; -import org.apache.airavata.common.utils.AiravataUtils; + import org.apache.airavata.common.utils.ServerSettings; -import org.apache.airavata.persistance.registry.jpa.ResourceType; -import org.apache.airavata.persistance.registry.jpa.resources.*; ++import org.apache.airavata.experiment.catalog.ResourceType; ++import org.apache.airavata.experiment.catalog.resources.*; + import org.apache.airavata.registry.cpi.RegistryException; + import org.apache.derby.drda.NetworkServerControl; + import org.slf4j.Logger; + import org.slf4j.LoggerFactory; + + import java.io.BufferedReader; + import java.io.IOException; + import java.io.InputStream; + import java.io.InputStreamReader; + import java.net.InetAddress; + import java.sql.*; + import java.util.StringTokenizer; + + public class Initialize { + private static final Logger logger = LoggerFactory.getLogger(Initialize.class); + public static final String DERBY_SERVER_MODE_SYS_PROPERTY = "derby.drda.startNetworkServer"; + public String scriptName = "registry-derby.sql"; + private NetworkServerControl server; + private static final String delimiter = ";"; + public static final String PERSISTANT_DATA = "Configuration"; + + public Initialize(String scriptName) { + this.scriptName = scriptName; + } + + public static boolean checkStringBufferEndsWith(StringBuffer buffer, String suffix) { + if (suffix.length() > buffer.length()) { + return false; + } + // this loop is done on purpose to avoid memory allocation performance + // problems on various JDKs + // StringBuffer.lastIndexOf() was introduced in jdk 1.4 and + // implementation is ok though does allocation/copying + // StringBuffer.toString().endsWith() does massive memory + // allocation/copying on JDK 1.5 + // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169 + int endIndex = suffix.length() - 1; + int bufferIndex = buffer.length() - 1; + while (endIndex >= 0) { + if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) { + return false; + } + bufferIndex--; + endIndex--; + } + return true; + } + + private static boolean isServerStarted(NetworkServerControl server, int ntries) + { + for (int i = 1; i <= ntries; i ++) + { + try { + Thread.sleep(500); + server.ping(); + return true; + } + catch (Exception e) { + if (i == ntries) + return false; + } + } + return false; + } + + public void initializeDB() throws SQLException{ + String jdbcUrl = null; + String jdbcUser = null; + String jdbcPassword = null; + try{ + jdbcUrl = ServerSettings.getSetting("registry.jdbc.url"); + jdbcUser = ServerSettings.getSetting("registry.jdbc.user"); + jdbcPassword = ServerSettings.getSetting("registry.jdbc.password"); + jdbcUrl = jdbcUrl + "?" + "user=" + jdbcUser + "&" + "password=" + jdbcPassword; + } catch (ApplicationSettingsException e) { + logger.error("Unable to read properties", e); + } + startDerbyInServerMode(); + if(!isServerStarted(server, 20)){ + throw new RuntimeException("Derby server cound not started within five seconds..."); + } + + Connection conn = null; + try { + Class.forName(Utils.getJDBCDriver()).newInstance(); + conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); + if (!isDatabaseStructureCreated(PERSISTANT_DATA, conn)) { + executeSQLScript(conn); + logger.info("New Database created for Registry"); + } else { + logger.debug("Database already created for Registry!"); + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new RuntimeException("Database failure", e); + } finally { + try { + if (conn != null){ + if (!conn.getAutoCommit()) { + conn.commit(); + } + conn.close(); + } + } catch (SQLException e) { + logger.error(e.getMessage(), e); + } + } + + try{ + GatewayResource gatewayResource = new GatewayResource(); + gatewayResource.setGatewayId(ServerSettings.getSetting("default.registry.gateway")); + gatewayResource.setGatewayName(ServerSettings.getSetting("default.registry.gateway")); + gatewayResource.setDomain("test-domain"); + gatewayResource.setEmailAddress("test-email"); + gatewayResource.save(); + + UserResource userResource = new UserResource(); + userResource.setUserName(ServerSettings.getSetting("default.registry.user")); + userResource.setPassword(ServerSettings.getSetting("default.registry.password")); + userResource.save(); + + WorkerResource workerResource = (WorkerResource) gatewayResource.create(ResourceType.GATEWAY_WORKER); + workerResource.setUser(userResource.getUserName()); + workerResource.save(); + + ProjectResource projectResource = (ProjectResource)workerResource.create(ResourceType.PROJECT); + projectResource.setGatewayId(gatewayResource.getGatewayId()); + projectResource.setId("default"); + projectResource.setName("default"); + projectResource.setWorker(workerResource); + projectResource.save(); + + + } catch (ApplicationSettingsException e) { + logger.error("Unable to read properties", e); + throw new SQLException(e.getMessage(), e); + } catch (RegistryException e) { + logger.error("Unable to save data to registry", e); + throw new SQLException(e.getMessage(), e); + } + } + + public static boolean isDatabaseStructureCreated(String tableName, Connection conn) { + try { + System.out.println("Running a query to test the database tables existence."); + // check whether the tables are already created with a query + Statement statement = null; + try { + statement = conn.createStatement(); + ResultSet rs = statement.executeQuery("select * from " + tableName); + if (rs != null) { + rs.close(); + } + } finally { + try { + if (statement != null) { + statement.close(); + } + } catch (SQLException e) { + return false; + } + } + } catch (SQLException e) { + return false; + } + + return true; + } + + private void executeSQLScript(Connection conn) throws Exception { + StringBuffer sql = new StringBuffer(); + BufferedReader reader = null; + try{ + + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(scriptName); + reader = new BufferedReader(new InputStreamReader(inputStream)); + String line; + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.startsWith("//")) { + continue; + } + if (line.startsWith("--")) { + continue; + } + StringTokenizer st = new StringTokenizer(line); + if (st.hasMoreTokens()) { + String token = st.nextToken(); + if ("REM".equalsIgnoreCase(token)) { + continue; + } + } + sql.append(" ").append(line); + + // SQL defines "--" as a comment to EOL + // and in Oracle it may contain a hint + // so we cannot just remove it, instead we must end it + if (line.indexOf("--") >= 0) { + sql.append("\n"); + } + if ((checkStringBufferEndsWith(sql, delimiter))) { + executeSQL(sql.substring(0, sql.length() - delimiter.length()), conn); + sql.replace(0, sql.length(), ""); + } + } + // Catch any statements not followed by ; + if (sql.length() > 0) { + executeSQL(sql.toString(), conn); + } + }catch (IOException e){ + logger.error("Error occurred while executing SQL script for creating Airavata database", e); + throw new Exception("Error occurred while executing SQL script for creating Airavata database", e); + }finally { + if (reader != null) { + reader.close(); + } + + } + + } + + private static void executeSQL(String sql, Connection conn) throws Exception { + // Check and ignore empty statements + if ("".equals(sql.trim())) { + return; + } + + Statement statement = null; + try { + logger.debug("SQL : " + sql); + + boolean ret; + int updateCount = 0, updateCountTotal = 0; + statement = conn.createStatement(); + ret = statement.execute(sql); + updateCount = statement.getUpdateCount(); + do { + if (!ret) { + if (updateCount != -1) { + updateCountTotal += updateCount; + } + } + ret = statement.getMoreResults(); + if (ret) { + updateCount = statement.getUpdateCount(); + } + } while (ret); + + logger.debug(sql + " : " + updateCountTotal + " rows affected"); + + SQLWarning warning = conn.getWarnings(); + while (warning != null) { + logger.warn(warning + " sql warning"); + warning = warning.getNextWarning(); + } + conn.clearWarnings(); + } catch (SQLException e) { + if (e.getSQLState().equals("X0Y32")) { + // eliminating the table already exception for the derby + // database + logger.info("Table Already Exists", e); + } else { + throw new Exception("Error occurred while executing : " + sql, e); + } + } finally { + if (statement != null) { + try { + statement.close(); + } catch (SQLException e) { + logger.error("Error occurred while closing result set.", e); + } + } + } + } + + private void startDerbyInServerMode() { + try { + System.setProperty(DERBY_SERVER_MODE_SYS_PROPERTY, "true"); + server = new NetworkServerControl(InetAddress.getByName(Utils.getHost()), + 20000, + Utils.getJDBCUser(), Utils.getJDBCPassword()); + java.io.PrintWriter consoleWriter = new java.io.PrintWriter(System.out, true); + server.start(consoleWriter); + } catch (IOException e) { + logger.error("Unable to start Apache derby in the server mode! Check whether " + + "specified port is available"); + } catch (Exception e) { + logger.error("Unable to start Apache derby in the server mode! Check whether " + + "specified port is available"); + } + + } + + public void stopDerbyServer() throws SQLException{ + try { + server.shutdown(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new SQLException("Error while stopping derby server", e); + } + } + } http://git-wip-us.apache.org/repos/asf/airavata/blob/b4ede9cb/modules/orchestrator/orchestrator-core/pom.xml ----------------------------------------------------------------------
