PageRenderTime 60ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/mercurial/src/org/netbeans/modules/mercurial/kenai/HgKenaiAccessor.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 153 lines | 76 code | 19 blank | 58 comment | 23 complexity | 70f965fa14475ffc11e6393d6a4a986c MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 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 2009 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.mercurial.kenai;
  43. import java.beans.PropertyChangeListener;
  44. import java.io.File;
  45. import java.net.PasswordAuthentication;
  46. import java.util.HashSet;
  47. import java.util.Set;
  48. import org.netbeans.modules.versioning.util.VCSKenaiAccessor;
  49. import org.netbeans.modules.versioning.util.VCSKenaiAccessor.KenaiUser;
  50. import org.openide.util.Lookup;
  51. /**
  52. *
  53. * @author Tomas Stupka, Ondra Vrabec
  54. */
  55. public class HgKenaiAccessor {
  56. private static HgKenaiAccessor instance;
  57. private VCSKenaiAccessor kenaiAccessor = null;
  58. private Set<String> queriedUrls = new HashSet<String>(5);
  59. private HgKenaiAccessor() {
  60. kenaiAccessor = Lookup.getDefault().lookup(VCSKenaiAccessor.class);
  61. }
  62. public static HgKenaiAccessor getInstance() {
  63. if(instance == null) {
  64. instance = new HgKenaiAccessor();
  65. }
  66. return instance;
  67. }
  68. public boolean isKenai(String url) {
  69. return kenaiAccessor != null && kenaiAccessor.isKenai(url);
  70. }
  71. public boolean isLoggedIntoKenai (String url) {
  72. return kenaiAccessor != null && kenaiAccessor.isLogged(url);
  73. }
  74. public PasswordAuthentication getPasswordAuthentication(String url, boolean forceRelogin) {
  75. if(kenaiAccessor != null) {
  76. if(forceRelogin && queriedUrls.contains(url)) {
  77. // we already queried the authentication for this url, but it didn't
  78. // seem to be accepted -> force a new login, the current user
  79. // might not be authorized for the given kenai project (url).
  80. if(!kenaiAccessor.showLogin()) {
  81. return null;
  82. }
  83. }
  84. queriedUrls.add(url);
  85. return kenaiAccessor.getPasswordAuthentication(url);
  86. } else {
  87. return null;
  88. }
  89. }
  90. public boolean isUserOnline(String user) {
  91. return kenaiAccessor != null ? kenaiAccessor.isUserOnline(user) : false;
  92. }
  93. public KenaiUser forName(String user, String url) {
  94. return kenaiAccessor != null ? kenaiAccessor.forName(user, url) : null;
  95. }
  96. public String getRevisionUrl(String repositoryUrl, String revision) {
  97. return kenaiAccessor == null ? null : kenaiAccessor.getRevisionUrl(repositoryUrl, revision);
  98. }
  99. private void removeVCSNoficationListener(PropertyChangeListener l) {
  100. if(kenaiAccessor != null) {
  101. kenaiAccessor.removeVCSNoficationListener(l);
  102. }
  103. }
  104. private void addVCSNoficationListener(PropertyChangeListener l) {
  105. if(kenaiAccessor != null) {
  106. kenaiAccessor.addVCSNoficationListener(l);
  107. }
  108. }
  109. public void registerVCSNoficationListener() {
  110. if("true".equals(System.getProperty("kenai.vcs.notifications.ignore"))) {
  111. return;
  112. }
  113. addVCSNoficationListener(new KenaiNotificationListener());
  114. }
  115. /**
  116. * Returns <code>false</code> only when the given repository is a kenai one, current user is logged in and does not have read access rights
  117. * @param repositoryUrl
  118. * @return
  119. */
  120. public boolean canRead (String repositoryUrl) {
  121. return isAuthorized(repositoryUrl, VCSKenaiAccessor.RepositoryActivity.READ);
  122. }
  123. /**
  124. * Returns <code>false</code> only when the given repository is a kenai one, current user is logged in and does not have push access rights
  125. * @param repositoryUrl
  126. * @return
  127. */
  128. public boolean canWrite (String repositoryUrl) {
  129. return isAuthorized(repositoryUrl, VCSKenaiAccessor.RepositoryActivity.WRITE);
  130. }
  131. private boolean isAuthorized (String repositoryUrl, VCSKenaiAccessor.RepositoryActivity permission) {
  132. return kenaiAccessor == null || !kenaiAccessor.isLogged(repositoryUrl) || kenaiAccessor.isAuthorized(repositoryUrl, permission);
  133. }
  134. }