Question

I am trying to makeAConnection with the server using this class. This class gets the list of parameters needed to perform operation on Images in HashMaps. Then in doInBackground , I perform the operations required on Image one by one. The code for one of the classes which is OVFImage Deployer is also pasted below

public class ImageDeployer extends SwingWorker<Boolean,String> {


    public ImageDeployer(){

    }

    public ImageDeployer(HashMap<String, String> volIDMap, HashMap<String, String> osMap) {
    // TODO Auto-generated constructor stub
    this.volIDMap = volIDMap;
    this.osMap = osMap;
    System.out.println(volIDMap);
    System.out.println(osMap);
    makeAConnection();
    try {
        doInBackground();
        System.out.println("Do In Background");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


public void makeAConnection(){

    inputFile = RESTEngine.getFilePath();
    Properties defaultProps = new Properties();
    try {
        fin = new FileInputStream(inputFile);
        defaultProps.load(fin);
        fin.close();
    }
    catch(FileNotFoundException e1){
        System.out.println("The properties file supposed to contain Authorization parameters was not found.");
        e1.printStackTrace();
        System.exit(-1); 
    }
    catch(IOException e1){
        System.out.println("An exception occured while trying to open the properties file");
        e1.printStackTrace();
        System.exit(-1);
    }
    // assign variables from Input file with default value as null
    user = defaultProps.getProperty("UserID", null);
    host = defaultProps.getProperty("PowerVC_IP_ADDRESS", null);
    password = defaultProps.getProperty("UserPass" ,null );

    jsch = new JSch();
    try {
        session = jsch.getSession(user, host, 22);
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel=session.openChannel("exec");
        channel.setInputStream(null);

        try {
            in = channel.getInputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Connection Successful");
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        System.out.println("Unable to connect");
        e.printStackTrace();
    }

}


@Override
protected Boolean doInBackground() throws Exception {

    ImageDeployer imageDeployer = new ImageDeployer();
    imageDeployer.makeAConnection();

    for(String imageName : volIDMap.keySet()){

        String volID = volIDMap.get(imageName);
        String oS = osMap.get(imageName);
        if (oS.equalsIgnoreCase("aix")){

            imageDeployer = new OVFImageDeployer(volID, oS, imageName);

        }
        // Other Cases depending upon the OS Type


    }
    return null;



}
}

The code for OVFImage Deployer

public class OVFImageDeployer extends PowerVCImageDeployer {

public OVFImageDeployer(String VolID,String oS,String imageName){

    String command="/usr/bin/powervc-devtools/powervc-devcli glance image-create json "+imageName+" "+oS+" "+VolID;

    try {


        ((ChannelExec)channel).setCommand(command);
        channel.connect();
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

Now when I run the code I get a NullPointerException on line ((ChannelExec)channel).setCommand(command). I know if I put makeAConnection after the try block in OVFImageDeployer the code would work, but then I don't want to make a connection again and again . I want a connection to be initialized just once and all operations to be performed using that connection only.

Was it helpful?

Solution

You should remove the call to doInBackground from inside the constructor of ImageDeployer:

public ImageDeployer(HashMap<String, String> volIDMap, HashMap<String, String> osMap) {
    ....
    makeAConnection();
    //doInBackground();
    ...
}

This will initialize the channel when you create an instance of ImageDeployer. And you can add channel to the list of constructor arguments of OVFImageDeployer:

public OVFImageDeployer(String VolID,String oS,String imageName, Channel channel){

this.channel = channel;
...
}

This will create an instance of OVFImageDeployer with the channel that is present in the ImageDeployer instance. You need to remove these two statements from inside the doInBackground method and pass channel along with the other parameters while constructing an instance of OVFImageDeployer:

@Override
protected Boolean doInBackground() throws Exception {

//ImageDeployer imageDeployer = new ImageDeployer();
//imageDeployer.makeAConnection();
...
ImageDeployer imageDeployer = new OVFImageDeployer(volID, oS, imageName, channel);
...
}

Now the client code can create an instance of ImageDeployer and can execute doInBackground on it:

ImageDeployer imageDeployer = new ImageDeployer();
imageDeployer.doInBackground();

With this, every time you create an instance of OVFImageDeployer inside the doInBackground method, you can use the same channel which was created by the makeAConnection method while constructing the ImageDeployer instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top