All pastes #373153 Raw Edit

Anonymous

public text v1 · immutable
#373153 ·published 2007-02-26 16:11 UTC
rendered paste body
/*
 * Copyright (c) 2006 Forschungszentrum Juelich GmbH
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * (1) Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer at the end. Redistributions in
 * binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution.
 *
 * (2) Neither the name of Forschungszentrum Juelich GmbH nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * DISCLAIMER
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * UiComputeDiscover.java
 *
 * Created on February 26, 2007, 3:47 PM
 *
 * Copyright (c) 2006 Forschungszentrum Juelich GmbH
 * All rights reserved.
 */

package org.nextgrid.udap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import org.apache.xmlbeans.XmlOptions;
import org.ggf.schemas.jsdl.x2005.x11.jsdl.CPUArchitectureType;
import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDefinitionDocument;
import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDefinitionType;
import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDescriptionType;
import org.ggf.schemas.jsdl.x2005.x11.jsdl.ProcessorArchitectureEnumeration;
import org.ggf.schemas.jsdl.x2005.x11.jsdl.RangeValueType;
import org.ggf.schemas.jsdl.x2005.x11.jsdl.ResourcesType;
import org.nextgrid.udap.userclient.UserClient;
import org.nextgrid.x2006.x05.udap.DescriptionType;
import org.nextgrid.x2006.x05.udap.ResourceType;
import org.nextgrid.x2006.x05.udap.UDAPRequestDocument;
import org.nextgrid.x2006.x05.udap.UDAPRequestDocument.UDAPRequest;
import org.nextgrid.x2006.x05.udap.UDAPResponseDocument;
import org.nextgrid.x2006.x05.udap.impl.ResourceTypeEnumerationTypeImpl;

/**
 *
 * @author Henning Mersch <h.mersch@fz-juelich.de>
 */
public class UiComputeDiscover {
    
    /** Creates a new instance of UiComputeDiscover */
    public UiComputeDiscover() {
    }
    
    Logger logger = Logger.getLogger(UiComputeDiscover.class.getName());
    private static final String DISPLAY_PAGE = "computediscover/computediscover.jsp";
    private static final String UDAPCLIENT = "http://localhost:7777/services/UdapClient";
    
    private String errorMessage = "";
    private String emptyList = "--- No Matching Services ---";
    
    private static final String IV_SERVICE_INSTALL_URI = "auto://42A6C1A0-B126-11DB-AC6F-C2B54450C558/home/autodeploy/gm_test.war";
    private static final String PROXY_DN = "C=UK/O=eoveri.com/CN=UDAP Upload Request";
    private static final String PORTAL_URI = "http://193.61.123.206:8080/DebutWS/services/PortalService";
    
    
    
    
    public static void main(String[] args) throws Exception{
        UiComputeDiscover d = new UiComputeDiscover();
        JobDefinitionDocument j = JobDefinitionDocument.Factory.newInstance();
        //JobDefinitionType jdt = d.formatQuery(j,"sparc","4","4","SunOS");
        JobDefinitionType jdt = d.formatQuery(j,"powerpc","","","");
        
        UDAPRequestDocument udap = UDAPRequestDocument.Factory.newInstance();
        UDAPRequest ureq = udap.addNewUDAPRequest();
        ureq.setId("programatic generated UDAP request ID");
            
            ResourceType r = ureq.addNewResource();
            r.setId("1st CR");
            r.setType(ResourceTypeEnumerationTypeImpl.Enum.forString("ComputeResource"));
            DescriptionType rd = r.addNewResourceDescription();
            rd.set(jdt.getJobDescription());
            
            rd.setDialect("http://schemas.ggf.org/jsdl/2005/11/jsdl");
            System.out.println("Generated UDAPRequest: \n"+udap.xmlText(new XmlOptions().setSavePrettyPrint()));
            
            UDAPResponseDocument udapResponse = UserClient.submitRequest(udap);
            System.out.println("got Response from client:\n"+udapResponse);
        
    } 

    
        /*
         ** Construct a query for the registry based on the values enterend on the screen.
         */
    private JobDefinitionType formatQuery(JobDefinitionDocument jsdl
            ,String architecture
            ,String txtRAM
            ,String txtDiskSpace
            ,String platform
            ) {
        
        JobDefinitionType jobdef = jsdl.addNewJobDefinition();
        JobDescriptionType jobdesc = jobdef.addNewJobDescription();
        ResourcesType res = jobdesc.addNewResources();
        
        // Platform
/*
          if (platform.length() > 0) {
            OperatingSystemTypeEnumeration.Enum osname = OperatingSystemTypeEnumeration.Enum.forString(platform);
            OperatingSystemType os = res.addNewOperatingSystem();
            os.setDescription(platform);
          }
 */
        
        // Architecture
        if (architecture.length() > 0) {
            ProcessorArchitectureEnumeration.Enum arch = ProcessorArchitectureEnumeration.Enum.forString(architecture);
            CPUArchitectureType cpuarch = res.addNewCPUArchitecture();
            cpuarch.setCPUArchitectureName(arch);
        }
        // RAM
        if (txtRAM.length() > 0) {
            if (Double.parseDouble(txtRAM) > 0) {
/*
              BoundaryType lower=BoundaryType.Factory.newInstance();
              lower.setDoubleValue(Double.parseDouble(txtRAM));
              RangeValueType rvt = RangeValueType.Factory.newInstance();
              rvt.setLowerBoundedRange(lower);
              rvt = res.addNewTotalPhysicalMemory();
              rvt.setLowerBoundedRange(lower);
 */
                RangeValueType ram = res.addNewTotalPhysicalMemory();
                ram.addNewExact().setEpsilon(Double.parseDouble(txtRAM));
                
            }
        }
        
        // Diskspace.
        if (txtDiskSpace.length() > 0) {
            if (Double.parseDouble(txtDiskSpace) > 0) {
                RangeValueType diskSpace = res.addNewTotalDiskSpace();
                diskSpace.addNewExact().setEpsilon(Double.parseDouble(txtDiskSpace));
            }
        }
        
        // Check if jobdef is valid - this seems to be the wrong way around i.e. Bugger! is valid???
        if (jobdef.validate()) {
            logger.info("VAlid jobdef");
        } else {
            logger.info("Bugger!");
        }
        
        return jobdef;
    }
    

        /*
         * Take a URI, strip out the path part and return it.
         */
    private String getUUID(String URI) {
        URL selectedURI;
        
        try {
            selectedURI = new URL(URI);
            // discard the / at the begining of the path.
            return selectedURI.getPath().substring(1);
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
            return "-No UUID-";
        }
    }
    /*
     * Read configuration document from file and return it as a string.
     */
    private String getConfigDocument(String UUID) throws Exception {
        logger.info("Reading Debut configuration document.");
        InputStream iv_cfg = getClass().getResourceAsStream("iv_service_config.xml");
        String request = inputStreamAsString(iv_cfg);
        request = request.replace("UUID_PARAMETER",UUID);
        iv_cfg.close();
        //logger.info("Generated CR-UDAPREQUEST:"+u);
        return request;
    }
        /*
         * Read an input stream and return it as a String.
         */
    private String inputStreamAsString(InputStream stream) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        StringBuilder sb = new StringBuilder();
        String line = null;
        
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        
        br.close();
        return sb.toString();
    }
    
}