Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PluginRegistry.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2014 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.models;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.concurrent.CopyOnWriteArrayList;
  22. import org.parboiled.common.StringUtils;
  23. import ro.fortsoft.pf4j.Version;
  24. /**
  25. * Represents a list of plugin registrations.
  26. */
  27. public class PluginRegistry implements Serializable {
  28. private static final long serialVersionUID = 1L;
  29. public final String name;
  30. public final List<PluginRegistration> registrations;
  31. public PluginRegistry(String name) {
  32. this.name = name;
  33. registrations = new CopyOnWriteArrayList<PluginRegistration>();
  34. }
  35. public void setup() {
  36. for (PluginRegistration reg : registrations) {
  37. reg.registry = name;
  38. }
  39. }
  40. public PluginRegistration lookup(String id) {
  41. for (PluginRegistration registration : registrations) {
  42. if (registration.id.equalsIgnoreCase(id)) {
  43. return registration;
  44. }
  45. }
  46. return null;
  47. }
  48. @Override
  49. public String toString() {
  50. return getClass().getSimpleName();
  51. }
  52. public static enum InstallState {
  53. NOT_INSTALLED, INSTALLED, UPDATE_AVAILABLE, UNKNOWN
  54. }
  55. /**
  56. * Represents a plugin registration.
  57. */
  58. public static class PluginRegistration implements Serializable {
  59. private static final long serialVersionUID = 1L;
  60. public final String id;
  61. public String description;
  62. public String provider;
  63. public String projectUrl;
  64. public transient String installedRelease;
  65. public transient String registry;
  66. public List<PluginRelease> releases;
  67. public PluginRegistration(String id) {
  68. this.id = id;
  69. this.releases = new ArrayList<PluginRelease>();
  70. }
  71. public PluginRelease getCurrentRelease(Version system) {
  72. PluginRelease current = null;
  73. Date date = new Date(0);
  74. for (PluginRelease pv : releases) {
  75. Version requires = Version.ZERO;
  76. if (!StringUtils.isEmpty(pv.requires)) {
  77. requires = Version.createVersion(pv.requires);
  78. }
  79. if (system.isZero() || system.atLeast(requires)) {
  80. if (pv.date.after(date)) {
  81. current = pv;
  82. date = pv.date;
  83. }
  84. }
  85. }
  86. return current;
  87. }
  88. public PluginRelease getRelease(String version) {
  89. for (PluginRelease pv : releases) {
  90. if (pv.version.equalsIgnoreCase(version)) {
  91. return pv;
  92. }
  93. }
  94. return null;
  95. }
  96. public InstallState getInstallState(Version system) {
  97. if (StringUtils.isEmpty(installedRelease)) {
  98. return InstallState.NOT_INSTALLED;
  99. }
  100. Version ir = Version.createVersion(installedRelease);
  101. Version cr = Version.ZERO;
  102. PluginRelease curr = getCurrentRelease(system);
  103. if (curr != null) {
  104. cr = Version.createVersion(curr.version);
  105. }
  106. switch (ir.compareTo(cr)) {
  107. case -1:
  108. return InstallState.UNKNOWN;
  109. case 1:
  110. return InstallState.UPDATE_AVAILABLE;
  111. default:
  112. return InstallState.INSTALLED;
  113. }
  114. }
  115. @Override
  116. public String toString() {
  117. return id;
  118. }
  119. }
  120. public static class PluginRelease implements Serializable, Comparable<PluginRelease> {
  121. private static final long serialVersionUID = 1L;
  122. public String version;
  123. public Date date;
  124. public String requires;
  125. public String url;
  126. @Override
  127. public int compareTo(PluginRelease o) {
  128. return Version.createVersion(version).compareTo(Version.createVersion(o.version));
  129. }
  130. }
  131. }