PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/platform-impl/src/com/intellij/remote/RemoteSdkCredentialsHolder.java

http://github.com/JetBrains/intellij-community
Java | 246 lines | 197 code | 42 blank | 7 comment | 40 complexity | 89db96eb7ca78295d8f0cac9b4a08d27 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
  2. package com.intellij.remote;
  3. import com.intellij.remote.ext.CredentialsManager;
  4. import com.intellij.util.PathMappingSettings;
  5. import org.jdom.Element;
  6. import org.jetbrains.annotations.NotNull;
  7. import org.jetbrains.annotations.Nullable;
  8. public class RemoteSdkCredentialsHolder extends RemoteCredentialsHolder implements RemoteSdkCredentials {
  9. @NotNull
  10. private final RemoteSdkPropertiesHolder myRemoteSdkProperties;
  11. public RemoteSdkCredentialsHolder(@NotNull final String defaultHelpersDirName) {
  12. myRemoteSdkProperties = new RemoteSdkPropertiesHolder(defaultHelpersDirName);
  13. }
  14. public static String constructSshCredentialsSdkFullPath(@NotNull RemoteSdkCredentials cred) {
  15. StringBuilder builder = new StringBuilder();
  16. if (cred.isRunAsRootViaSudo()) {
  17. builder.append("sudo+");
  18. }
  19. return builder
  20. .append(getCredentialsString(cred))
  21. .append(cred.getInterpreterPath())
  22. .toString();
  23. }
  24. /**
  25. * Extracts interpreter path from full path generated by method getFullInterpreterPath
  26. * Returns fullPath as fallback
  27. * <p/>
  28. * Based on the statement that host can't contain colon(:) symbol
  29. */
  30. public static String getInterpreterPathFromFullPath(String fullPath) {
  31. if (fullPath.startsWith(SSH_PREFIX)) {
  32. fullPath = fullPath.substring(SSH_PREFIX.length());
  33. int index = fullPath.indexOf(":");
  34. if (index != -1 && index < fullPath.length()) {
  35. fullPath = fullPath.substring(index + 1); // it is like 8080/home/user or 8080C:\Windows
  36. index = 0;
  37. while (index < fullPath.length() && Character.isDigit(fullPath.charAt(index))) {
  38. index++;
  39. }
  40. if (index < fullPath.length()) {
  41. return fullPath.substring(index);
  42. }
  43. }
  44. }
  45. return fullPath;
  46. }
  47. @NotNull
  48. public RemoteSdkPropertiesHolder getRemoteSdkProperties() {
  49. return myRemoteSdkProperties;
  50. }
  51. @Override
  52. public String getInterpreterPath() {
  53. return myRemoteSdkProperties.getInterpreterPath();
  54. }
  55. @Override
  56. public void setInterpreterPath(String interpreterPath) {
  57. myRemoteSdkProperties.setInterpreterPath(interpreterPath);
  58. }
  59. @Override
  60. public String getHelpersPath() {
  61. return myRemoteSdkProperties.getHelpersPath();
  62. }
  63. @Override
  64. public void setHelpersPath(String helpersPath) {
  65. myRemoteSdkProperties.setHelpersPath(helpersPath);
  66. }
  67. @Override
  68. public String getDefaultHelpersName() {
  69. return myRemoteSdkProperties.getDefaultHelpersName();
  70. }
  71. @NotNull
  72. @Override
  73. public PathMappingSettings getPathMappings() {
  74. return myRemoteSdkProperties.getPathMappings();
  75. }
  76. @Override
  77. public void setPathMappings(@Nullable PathMappingSettings pathMappings) {
  78. myRemoteSdkProperties.setPathMappings(pathMappings);
  79. }
  80. @Override
  81. public boolean isHelpersVersionChecked() {
  82. return myRemoteSdkProperties.isHelpersVersionChecked();
  83. }
  84. @Override
  85. public void setHelpersVersionChecked(boolean helpersVersionChecked) {
  86. myRemoteSdkProperties.setHelpersVersionChecked(helpersVersionChecked);
  87. }
  88. @Override
  89. public String getFullInterpreterPath() {
  90. return constructSshCredentialsSdkFullPath(this);
  91. }
  92. @Override
  93. public void setSdkId(String sdkId) {
  94. myRemoteSdkProperties.setSdkId(sdkId);
  95. }
  96. @Override
  97. public String getSdkId() {
  98. return myRemoteSdkProperties.getSdkId();
  99. }
  100. @Override
  101. public boolean isInitialized() {
  102. return myRemoteSdkProperties.isInitialized();
  103. }
  104. @Override
  105. public void setInitialized(boolean initialized) {
  106. myRemoteSdkProperties.setInitialized(initialized);
  107. }
  108. @Override
  109. public boolean isValid() {
  110. return myRemoteSdkProperties.isValid();
  111. }
  112. @Override
  113. public void setValid(boolean valid) {
  114. myRemoteSdkProperties.setValid(valid);
  115. }
  116. @Override
  117. public boolean isRunAsRootViaSudo() {
  118. return myRemoteSdkProperties.isRunAsRootViaSudo();
  119. }
  120. @Override
  121. public void setRunAsRootViaSudo(boolean runAsRootViaSudo) {
  122. myRemoteSdkProperties.setRunAsRootViaSudo(runAsRootViaSudo);
  123. }
  124. public static boolean isRemoteSdk(@Nullable String path) {
  125. if (path != null) {
  126. for (CredentialsType type : CredentialsManager.getInstance().getAllTypes()) {
  127. if (type.hasPrefix(path)) {
  128. return true;
  129. }
  130. }
  131. }
  132. return false;
  133. }
  134. @Override
  135. public void load(Element element) {
  136. super.load(element);
  137. myRemoteSdkProperties.load(element);
  138. }
  139. @Override
  140. public void save(Element rootElement) {
  141. super.save(rootElement);
  142. myRemoteSdkProperties.save(rootElement);
  143. }
  144. @Override
  145. public boolean equals(Object o) {
  146. if (this == o) return true;
  147. if (o == null || getClass() != o.getClass()) return false;
  148. RemoteSdkCredentialsHolder holder = (RemoteSdkCredentialsHolder)o;
  149. if (!getLiteralPort().equals(holder.getLiteralPort())) return false;
  150. if (isStorePassphrase() != holder.isStorePassphrase()) return false;
  151. if (isStorePassword() != holder.isStorePassword()) return false;
  152. if (getAuthType() != holder.getAuthType()) return false;
  153. if (!getHost().equals(holder.getHost())) return false;
  154. if (getPassphrase() != null ? !getPassphrase().equals(holder.getPassphrase()) : holder.getPassphrase() != null) return false;
  155. if (getPassword() != null ? !getPassword().equals(holder.getPassword()) : holder.getPassword() != null) return false;
  156. if (!getPrivateKeyFile().equals(holder.getPrivateKeyFile())) {
  157. return false;
  158. }
  159. if (getUserName() != null ? !getUserName().equals(holder.getUserName()) : holder.getUserName() != null) return false;
  160. if (!myRemoteSdkProperties.equals(holder.myRemoteSdkProperties)) {
  161. return false;
  162. }
  163. return true;
  164. }
  165. @Override
  166. public int hashCode() {
  167. int result = getHost().hashCode();
  168. result = 31 * result + getLiteralPort().hashCode();
  169. result = 31 * result + (getUserName() != null ? getUserName().hashCode() : 0);
  170. result = 31 * result + (getPassword() != null ? getPassword().hashCode() : 0);
  171. result = 31 * result + getAuthType().hashCode();
  172. result = 31 * result + getPrivateKeyFile().hashCode();
  173. result = 31 * result + (getPassphrase() != null ? getPassphrase().hashCode() : 0);
  174. result = 31 * result + (isStorePassword() ? 1 : 0);
  175. result = 31 * result + (isStorePassphrase() ? 1 : 0);
  176. result = 31 * result + myRemoteSdkProperties.hashCode();
  177. return result;
  178. }
  179. @Override
  180. public String toString() {
  181. return "RemoteSdkDataHolder" +
  182. "{getHost()='" +
  183. getHost() +
  184. '\'' +
  185. ", getLiteralPort()=" +
  186. getLiteralPort() +
  187. ", getUserName()='" +
  188. getUserName() +
  189. '\'' +
  190. ", myInterpreterPath='" +
  191. getInterpreterPath() +
  192. '\'' +
  193. ", isRunAsRootViaSudo=" +
  194. isRunAsRootViaSudo() +
  195. ", myHelpersPath='" +
  196. getHelpersPath() +
  197. '\'' +
  198. '}';
  199. }
  200. public void copyRemoteSdkCredentialsTo(RemoteSdkCredentialsHolder to) {
  201. super.copyRemoteCredentialsTo(to);
  202. myRemoteSdkProperties.copyTo(to.getRemoteSdkProperties());
  203. }
  204. }