PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/cnd.debugger.common2/src/org/netbeans/modules/cnd/debugger/common2/debugger/remote/CustomizableHost.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 366 lines | 168 code | 49 blank | 149 comment | 12 complexity | fa5bdc5ffd914f9e32a80f2a70bc262c MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2011 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.cnd.debugger.common2.debugger.remote;
  43. import org.netbeans.modules.cnd.debugger.common2.utils.IpeUtils;
  44. import org.netbeans.modules.cnd.debugger.common2.utils.masterdetail.Record;
  45. import org.netbeans.modules.cnd.debugger.common2.utils.options.OptionSet;
  46. import org.netbeans.modules.cnd.debugger.common2.utils.options.OptionSetOwner;
  47. import org.netbeans.modules.cnd.debugger.common2.utils.options.OptionValue;
  48. import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
  49. import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory;
  50. import org.openide.nodes.Sheet;
  51. /**
  52. *
  53. * @author Egor Ushakov
  54. */
  55. public class CustomizableHost extends Host implements Record, OptionSetOwner {
  56. private OptionSet options = new HostOptionSet();
  57. private static final String default_location = System.getProperty("spro.home");
  58. /**
  59. * Create a Default Host with the following properties.
  60. * host: localhost
  61. * platform: <empty>
  62. * studio location: "spro.home" property
  63. * user: "user.name" property
  64. * rememberPassword:
  65. */
  66. public CustomizableHost() {
  67. setHostName(localhost);
  68. setRemoteStudioLocation(default_location);
  69. }
  70. /**
  71. * The key used in the hosts DB.
  72. */
  73. // interface Record
  74. public String getKey() {
  75. return getHostName();
  76. }
  77. // interface Record
  78. public void setKey(String newKey) {
  79. // Do allow setting of an archetypes key because otherwise
  80. // it's duplicate would be an archetype too.
  81. setHostName(newKey);
  82. }
  83. // interface Record
  84. public boolean matches(String key) {
  85. return IpeUtils.sameString(getKey(), key);
  86. }
  87. // interface Record
  88. public boolean isArchetype() {
  89. return localhost.equals(getHostName()); // NOI18N
  90. }
  91. /**
  92. * Convert this to an ExecutionEnvironment.
  93. */
  94. public ExecutionEnvironment executionEnvironment() {
  95. ExecutionEnvironment ee;
  96. if (isRemote())
  97. ee = ExecutionEnvironmentFactory.createNew(getHostLogin(), getHostName(), getPortNum());
  98. else
  99. ee = ExecutionEnvironmentFactory.getLocal();
  100. return ee;
  101. }
  102. private static class LocalHostNameHolder {
  103. private static final String name = getLocalHost();
  104. private static String getLocalHost() {
  105. try {
  106. java.net.InetAddress addr = java.net.InetAddress.getLocalHost();
  107. return addr.getHostName();
  108. } catch (java.net.UnknownHostException x) {
  109. return "<unknown>"; // NOI18N
  110. }
  111. }
  112. }
  113. /**
  114. * Return the current actual primary hostname (not nicknames).
  115. */
  116. private static String getLocalHost() {
  117. return LocalHostNameHolder.name;
  118. }
  119. public boolean isRemote() {
  120. return isRemote(getHostName());
  121. }
  122. public CustomizableHost(OptionSet options) {
  123. this.options = options;
  124. }
  125. public void setPlatform(Platform platform) {
  126. setPlatformName(platform.name());
  127. }
  128. /*
  129. * Stored in option property and display to users as the form of
  130. * "Solaris_Sparc", "Solaris_x86" , "Linux_x86"
  131. */
  132. public String getPlatformName() {
  133. String platForm = getHostOption("platform"); // NOI18N
  134. return platForm;
  135. }
  136. public void setPlatformName(String platform) {
  137. setHostOption("platform", platform); // NOI18N
  138. }
  139. public String getHostName() {
  140. String hostName = getHostOption("host_name"); // NOI18N
  141. return hostName;
  142. }
  143. public int getPortNum() {
  144. return Integer.parseInt(getHostOption("ssh_port")); // NOI18N
  145. }
  146. public boolean isRememberPassword() {
  147. return "on".equals(getHostOption("remember_password"));// NOI18N
  148. }
  149. public void setRememberPassword(boolean rememberPassword) {
  150. if (rememberPassword)
  151. setHostOption("remember_password", "on"); // NOI18N
  152. else
  153. setHostOption("remember_password", "off"); // NOI18N
  154. }
  155. public void setHostName(String hostName) {
  156. setHostOption("host_name", hostName); // NOI18N
  157. }
  158. /**
  159. * Under IDE returns user@host|localhost; under dbxtool returns host.
  160. * This helps use interoperate with CND's convention of using user@host
  161. * for hostname.
  162. */
  163. public String getHostKey() {
  164. /*
  165. if (DebuggerManager.isStandalone()) {
  166. return getHostName();
  167. } else {
  168. *
  169. */
  170. if (isRemote()) {
  171. return getHostLogin() + "@" + getHostName() + // NOI18N
  172. ":" + getPortNum(); // NOI18N
  173. } else {
  174. return getHostName();
  175. }
  176. //}
  177. }
  178. public String getHostLogin() {
  179. String loginName = getHostOption("login_name"); // NOI18N
  180. return loginName;
  181. }
  182. public void setHostLogin(String loginName) {
  183. setHostOption("login_name", loginName); // NOI18N
  184. }
  185. public String getRemoteStudioLocation() {
  186. String studioLocation = getHostOption("studio_location"); // NOI18N
  187. if (Log.Remote.host) {
  188. System.out.printf("Host.getRemoteStudioLocation() -> %s\n", studioLocation); // NOI18N
  189. }
  190. return studioLocation;
  191. }
  192. public void setRemoteStudioLocation(String studioLocation) {
  193. setHostOption("studio_location", studioLocation); // NOI18N
  194. }
  195. public void setSecuritySettings(SecuritySettings ss) {
  196. setHostOption("ssh_port", Integer.toString(ss.sshPort())); // NOI18N
  197. }
  198. public SecuritySettings getSecuritySettings() {
  199. return new SecuritySettings(Integer.parseInt(getHostOption("ssh_port")), null); // NOI18N
  200. }
  201. // interface Record
  202. public CustomizableHost cloneRecord() {
  203. return new CustomizableHost(options.makeCopy());
  204. }
  205. // interface Record
  206. public String displayName() {
  207. if (getHostName().equals("localhost")) // NOI18N
  208. return getHostName() + " (" + getLocalHost() + ", " + getPlatformName() + " )"; // NOI18N
  209. else
  210. return getHostName() + " (" + getPlatformName() + " )"; // NOI18N
  211. }
  212. public void setHostOption(String name, String value) {
  213. if (Log.Remote.host && "studio_location".equals(name)) { // NOI18N
  214. System.out.printf("Host.setHostOption(%s, %s)\n", name, value); // NOI18N
  215. }
  216. OptionValue o = options.byName(name);
  217. // OLD needValidate = true;
  218. invalidateResources();
  219. if (o != null)
  220. o.set(value);
  221. }
  222. public String getHostOption(String name) {
  223. OptionValue o = options.byName(name);
  224. if (o != null)
  225. return o.get();
  226. return null;
  227. }
  228. public void assign(CustomizableHost that) {
  229. // OLD needValidate = true;
  230. invalidateResources();
  231. this.options.assign(that.options);
  232. }
  233. // interface OptionSetOwner
  234. public OptionSet getOptions() {
  235. return options;
  236. }
  237. /* LATER
  238. Attempt at not using Option mechanisms for property sheet.
  239. Not quite there. Editors aren't quite the same and we impose special
  240. semantics (See HostOption).
  241. private final Node.Property createHostNameNodeProp(Object o) throws NoSuchMethodException {
  242. Node.Property p = new PropertySupport.Reflection<String>(o,
  243. String.class,
  244. "getHostName",
  245. "setHostName");
  246. p.setName("HostName");
  247. p.setDisplayName("host-name");
  248. p.setShortDescription("Shost-name");
  249. return p;
  250. }
  251. private final Node.Property createPlatformNodeProp(Object o) throws NoSuchMethodException {
  252. Node.Property p = new PropertySupport.Reflection<Platform>(o,
  253. Platform.class,
  254. "getPlatform",
  255. "setPlatform");
  256. p.setName("Platform");
  257. p.setDisplayName("platform");
  258. p.setShortDescription("Splatform");
  259. return p;
  260. }
  261. private final Node.Property createLocationNodeProp(Object o) throws NoSuchMethodException {
  262. Node.Property p = new PropertySupport.Reflection<String>(o,
  263. String.class,
  264. "getRemoteStudioLocation",
  265. "setRemoteStudioLocation");
  266. p.setName("RemoteStudioLocation");
  267. p.setDisplayName("studio-location");
  268. p.setShortDescription("Sstudio-location");
  269. return p;
  270. }
  271. private final Node.Property createUsernameNodeProp(Object o) throws NoSuchMethodException {
  272. Node.Property p = new PropertySupport.Reflection<String>(o,
  273. String.class,
  274. "getHostLogin",
  275. "setHostLogin");
  276. p.setName("HostLogin");
  277. p.setDisplayName("host-login");
  278. p.setShortDescription("Shost-login");
  279. return p;
  280. }
  281. */
  282. public Sheet getSheet() {
  283. Sheet sheet = new Sheet();
  284. Sheet.Set set;
  285. set = new Sheet.Set();
  286. set.setName("General"); // NOI18N
  287. set.setDisplayName("General"); // FIXUP I18N // NOI18N
  288. set.setShortDescription("General"); // FIXUP I18N // NOI18N
  289. set.put(HostOption.HOST_PROP_HOSTNAME.createNodeProp(this));
  290. set.put(HostOption.HOST_PROP_PLATFORM.createNodeProp(this));
  291. set.put(HostOption.HOST_PROP_LOCATION.createNodeProp(this));
  292. set.put(HostOption.HOST_PROP_LOGINNAME.createNodeProp(this));
  293. set.put(HostOption.HOST_PROP_SSH_PORT.createNodeProp(this));
  294. set.put(HostOption.HOST_PROP_REMEMBER_PASSWORD.createNodeProp(this));
  295. /* LATER
  296. try {
  297. set.put(createHostNameNodeProp(this));
  298. set.put(createPlatformNodeProp(this));
  299. set.put(createLocationNodeProp(this));
  300. set.put(creFateUsernameNodeProp(this));
  301. } catch (NoSuchMethodException ex) {
  302. Exceptions.printStackTrace(ex);
  303. }
  304. */
  305. sheet.put(set);
  306. return sheet;
  307. }
  308. @Override
  309. public String toString() {
  310. return "CustomizableHost: " + getHostName(); //NOI18N
  311. }
  312. }