尝试动态实例化类

时间:2013-09-30 11:30:13

标签: java inheritance casting factory-pattern jsch

我正在尝试使用此类与服务器进行连接。此类获取在HashMaps中对Images执行操作所需的参数列表。然后在doInBackground中,我逐个执行Image所需的操作。其中一个类OVFImage Deployer的代码也粘贴在

下面
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;



}
}

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();
    }

}

}

现在,当我运行代码时,我会在NullPointerException行上获得((ChannelExec)channel).setCommand(command)。 我知道如果我在makeAConnection中的try块之后放OVFImageDeployer代码可以工作,但我不想一次又一次地建立连接。我希望只连接一次连接,并且只使用该连接执行所有操作。

1 个答案:

答案 0 :(得分:1)

您应该从doInBackground的构造函数中删除对ImageDeployer的调用:

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

这将在您创建channel的实例时初始化ImageDeployer。您可以将channel添加到OVFImageDeployer

的构造函数参数列表中
public OVFImageDeployer(String VolID,String oS,String imageName, Channel channel){

this.channel = channel;
...
}

这将创建一个OVFImageDeployer的实例,channel实例中存在ImageDeployer。您需要从doInBackground方法中删除这两个语句,并在构造channel的实例时将OVFImageDeployer与其他参数一起传递:

@Override
protected Boolean doInBackground() throws Exception {

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

现在,客户端代码可以创建ImageDeployer的实例并可以对其执行doInBackground

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

有了这个,每次在OVFImageDeployer方法中创建doInBackground的实例时,您都可以使用channel方法创建的makeAConnection ImageDeployer实例。

相关问题