gpt4 book ai didi

java - 尝试动态实例化类

转载 作者:太空宇宙 更新时间:2023-11-04 07:22:28 26 4
gpt4 key购买 nike

我正在尝试使用此类与服务器建立连接。此类获取对 HashMap 中的图像执行操作所需的参数列表。然后在 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();
}

}

}

现在,当我运行代码时,我在 ((ChannelExec)channel).setCommand(command) 行上收到 NullPointerException。我知道如果我将 makeAConnection 放在 OVFImageDeployer 中的 try block 之后,代码就会起作用,但我不想一次又一次地建立连接。我希望连接仅初始化一次,并且仅使用该连接执行所有操作。

最佳答案

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

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

这将在您创建 ImageDeployer 实例时初始化 channel 。您可以将 channel 添加到 OVFImageDeployer 的构造函数参数列表中:

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

this.channel = channel;
...
}

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

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

这样,每次在 doInBackground 方法内创建 OVFImageDeployer 实例时,您都可以在构造 ImageDeployer 实例时使用由 makeAConnection 方法创建的相同 channel

关于java - 尝试动态实例化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19093130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com