/platform/remote-core/src/remote/RemoteSdkCredentialsHolder.java

https://github.com/JetBrains/intellij-community · Java · 249 lines · 200 code · 42 blank · 7 comment · 40 complexity · 7b9b0bc8f45ca1f625f9d43f90257ce1 MD5 · raw file

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