1
22
23 package com.liferay.portal.plugin;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.plugin.PluginPackage;
29 import com.liferay.portal.kernel.plugin.RemotePluginPackageRepository;
30 import com.liferay.portal.kernel.search.BooleanClauseOccur;
31 import com.liferay.portal.kernel.search.BooleanQuery;
32 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
33 import com.liferay.portal.kernel.search.Field;
34 import com.liferay.portal.kernel.search.Hits;
35 import com.liferay.portal.kernel.search.Query;
36 import com.liferay.portal.kernel.search.SearchEngineUtil;
37 import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
38 import com.liferay.portal.kernel.util.ArrayUtil;
39 import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
40 import com.liferay.portal.kernel.util.GetterUtil;
41 import com.liferay.portal.kernel.util.HtmlUtil;
42 import com.liferay.portal.kernel.util.Http;
43 import com.liferay.portal.kernel.util.HttpUtil;
44 import com.liferay.portal.kernel.util.PropsKeys;
45 import com.liferay.portal.kernel.util.ReleaseInfo;
46 import com.liferay.portal.kernel.util.StringPool;
47 import com.liferay.portal.kernel.util.StringUtil;
48 import com.liferay.portal.kernel.util.Time;
49 import com.liferay.portal.kernel.util.Validator;
50 import com.liferay.portal.kernel.xml.Attribute;
51 import com.liferay.portal.kernel.xml.Document;
52 import com.liferay.portal.kernel.xml.DocumentException;
53 import com.liferay.portal.kernel.xml.Element;
54 import com.liferay.portal.kernel.xml.SAXReaderUtil;
55 import com.liferay.portal.model.CompanyConstants;
56 import com.liferay.portal.model.Plugin;
57 import com.liferay.portal.util.HttpImpl;
58 import com.liferay.portal.util.PrefsPropsUtil;
59 import com.liferay.portal.util.PropsValues;
60 import com.liferay.util.License;
61 import com.liferay.util.Screenshot;
62 import com.liferay.util.Version;
63
64 import java.io.IOException;
65
66 import java.net.MalformedURLException;
67
68 import java.text.DateFormat;
69
70 import java.util.ArrayList;
71 import java.util.Arrays;
72 import java.util.Collection;
73 import java.util.Date;
74 import java.util.HashMap;
75 import java.util.Iterator;
76 import java.util.List;
77 import java.util.Locale;
78 import java.util.Map;
79 import java.util.Properties;
80 import java.util.Set;
81 import java.util.TreeSet;
82
83 import javax.servlet.http.HttpServletResponse;
84
85 import org.apache.commons.httpclient.HostConfiguration;
86 import org.apache.commons.httpclient.HttpClient;
87 import org.apache.commons.httpclient.methods.GetMethod;
88 import org.apache.commons.lang.time.StopWatch;
89
90
97 public class PluginPackageUtil {
98
99 public static final String REPOSITORY_XML_FILENAME_PREFIX =
100 "liferay-plugin-repository";
101
102 public static final String REPOSITORY_XML_FILENAME_EXTENSION =
103 "xml";
104
105 public static void endPluginPackageInstallation(String preliminaryContext) {
106 _instance._endPluginPackageInstallation(preliminaryContext);
107 }
108
109 public static List<PluginPackage> getAllAvailablePluginPackages()
110 throws PluginPackageException {
111
112 return _instance._getAllAvailablePluginPackages();
113 }
114
115 public static Collection<String> getAvailableTags() {
116 return _instance._getAvailableTags();
117 }
118
119 public static List<PluginPackage> getInstalledPluginPackages() {
120 return _instance._getInstalledPluginPackages();
121 }
122
123 public static PluginPackage getLatestAvailablePluginPackage(
124 String groupId, String artifactId)
125 throws SystemException {
126
127 return _instance._getLatestAvailablePluginPackage(groupId, artifactId);
128 }
129
130 public static PluginPackage getLatestInstalledPluginPackage(
131 String groupId, String artifactId) {
132
133 return _instance._getLatestInstalledPluginPackage(groupId, artifactId);
134 }
135
136 public static Date getLastUpdateDate() {
137 return _instance._getLastUpdateDate();
138 }
139
140 public static PluginPackage getPluginPackageByModuleId(
141 String moduleId, String repositoryURL)
142 throws PluginPackageException {
143
144 return _instance._getPluginPackageByModuleId(moduleId, repositoryURL);
145 }
146
147 public static PluginPackage getPluginPackageByURL(String url)
148 throws PluginPackageException {
149
150 return _instance._getPluginPackageByURL(url);
151 }
152
153 public static RemotePluginPackageRepository getRepository(
154 String repositoryURL)
155 throws PluginPackageException {
156
157 return _instance._getRepository(repositoryURL);
158 }
159
160 public static String[] getRepositoryURLs() throws PluginPackageException {
161 return _instance._getRepositoryURLs();
162 }
163
164 public static String[] getSupportedTypes() {
165 return _instance._getSupportedTypes();
166 }
167
168 public static boolean isCurrentVersionSupported(List<String> versions) {
169 return _instance._isCurrentVersionSupported(versions);
170 }
171
172 public static boolean isIgnored(PluginPackage pluginPackage)
173 throws SystemException {
174
175 return _instance._isIgnored(pluginPackage);
176 }
177
178 public static boolean isInstallationInProcess(String context) {
179 return _instance._isInstallationInProcess(context);
180 }
181
182 public static boolean isTrusted(String repositoryURL)
183 throws PluginPackageException {
184
185 return _instance._isTrusted(repositoryURL);
186 }
187
188 public static boolean isUpdateAvailable() throws SystemException {
189 return _instance._isUpdateAvailable();
190 }
191
192 public static PluginPackage readPluginPackageProperties(
193 String displayName, Properties props) {
194
195 return _instance._readPluginPackageProperties(displayName, props);
196 }
197
198 public static PluginPackage readPluginPackageXml(String xml)
199 throws DocumentException {
200
201 return _instance._readPluginPackageXml(xml);
202 }
203
204 public static PluginPackage readPluginPackageXml(Element pluginPackageEl) {
205 return _instance._readPluginPackageXml(pluginPackageEl);
206 }
207
208 public static void refreshUpdatesAvailableCache() {
209 _instance._refreshUpdatesAvailableCache();
210 }
211
212 public static void reIndex() throws SystemException {
213 _instance._reIndex();
214 }
215
216 public static RepositoryReport reloadRepositories() throws SystemException {
217 return _instance._reloadRepositories();
218 }
219
220 public static void registerInstalledPluginPackage(
221 PluginPackage pluginPackage) {
222
223 _instance._registerInstalledPluginPackage(pluginPackage);
224 }
225
226 public static void registerPluginPackageInstallation(
227 String preliminaryContext) {
228
229 _instance._registerPluginPackageInstallation(preliminaryContext);
230 }
231
232 public static Hits search(
233 String keywords, String type, String tag, String license,
234 String repositoryURL, String status, int start, int end)
235 throws SystemException {
236
237 return _instance._search(
238 keywords, type, tag, license, repositoryURL, status, start, end);
239 }
240
241 public static void unregisterInstalledPluginPackage(
242 PluginPackage pluginPackage) {
243
244 _instance._unregisterInstalledPluginPackage(pluginPackage);
245 }
246
247 public static void updateInstallingPluginPackage(
248 String preliminaryContext, PluginPackage pluginPackage) {
249
250 _instance._updateInstallingPluginPackage(
251 preliminaryContext, pluginPackage);
252 }
253
254 private PluginPackageUtil() {
255 _installedPluginPackages = new LocalPluginPackageRepository();
256 _repositoryCache = new HashMap<String, RemotePluginPackageRepository>();
257 _availableTagsCache = new TreeSet<String>();
258 }
259
260 private void _checkRepositories(String repositoryURL)
261 throws PluginPackageException {
262
263 String[] repositoryURLs = null;
264
265 if (Validator.isNotNull(repositoryURL)) {
266 repositoryURLs = new String[] {repositoryURL};
267 }
268 else {
269 repositoryURLs = _getRepositoryURLs();
270 }
271
272 for (int i = 0; i < repositoryURLs.length; i++) {
273 _getRepository(repositoryURLs[i]);
274 }
275 }
276
277 private void _endPluginPackageInstallation(String preliminaryContext) {
278 _installedPluginPackages.unregisterPluginPackageInstallation(
279 preliminaryContext);
280 }
281
282 private PluginPackage _findLatestVersion(
283 List<PluginPackage> pluginPackages) {
284
285 PluginPackage latestPluginPackage = null;
286
287 for (PluginPackage pluginPackage : pluginPackages) {
288 if ((latestPluginPackage == null) ||
289 (pluginPackage.isLaterVersionThan(latestPluginPackage))) {
290
291 latestPluginPackage = pluginPackage;
292 }
293 }
294
295 return latestPluginPackage;
296 }
297
298 private List<PluginPackage> _getAllAvailablePluginPackages()
299 throws PluginPackageException {
300
301 List<PluginPackage> pluginPackages = new ArrayList<PluginPackage>();
302
303 String[] repositoryURLs = _getRepositoryURLs();
304
305 for (int i = 0; i < repositoryURLs.length; i++) {
306 try {
307 RemotePluginPackageRepository repository =
308 _getRepository(repositoryURLs[i]);
309
310 pluginPackages.addAll(repository.getPluginPackages());
311 }
312 catch (PluginPackageException ppe) {
313 String message = ppe.getMessage();
314
315 if (message.startsWith("Unable to communicate")) {
316 if (_log.isWarnEnabled()) {
317 _log.warn(message);
318 }
319 }
320 else {
321 _log.error(message);
322 }
323 }
324 }
325
326 return pluginPackages;
327 }
328
329 private List<PluginPackage> _getAvailablePluginPackages(
330 String groupId, String artifactId)
331 throws PluginPackageException {
332
333 List<PluginPackage> pluginPackages = new ArrayList<PluginPackage>();
334
335 String[] repositoryURLs = _getRepositoryURLs();
336
337 for (int i = 0; i < repositoryURLs.length; i++) {
338 RemotePluginPackageRepository repository =
339 _getRepository(repositoryURLs[i]);
340
341 List<PluginPackage> curPluginPackages =
342 repository.findPluginsByGroupIdAndArtifactId(
343 groupId, artifactId);
344
345 if (curPluginPackages != null) {
346 pluginPackages.addAll(curPluginPackages);
347 }
348 }
349
350 return pluginPackages;
351 }
352
353 private Collection<String> _getAvailableTags() {
354 return _availableTagsCache;
355 }
356
357 private List<PluginPackage> _getInstalledPluginPackages() {
358 return _installedPluginPackages.getSortedPluginPackages();
359 }
360
361 private PluginPackage _getLatestAvailablePluginPackage(
362 String groupId, String artifactId)
363 throws SystemException {
364
365 List<PluginPackage> pluginPackages = _getAvailablePluginPackages(
366 groupId, artifactId);
367
368 return _findLatestVersion(pluginPackages);
369 }
370
371 private PluginPackage _getLatestInstalledPluginPackage(
372 String groupId, String artifactId) {
373
374 return _installedPluginPackages.getLatestPluginPackage(
375 groupId, artifactId);
376 }
377
378 private Date _getLastUpdateDate() {
379 return _lastUpdateDate;
380 }
381
382 private PluginPackage _getPluginPackageByModuleId(
383 String moduleId, String repositoryURL)
384 throws PluginPackageException {
385
386 RemotePluginPackageRepository repository = _getRepository(
387 repositoryURL);
388
389 return repository.findPluginPackageByModuleId(moduleId);
390 }
391
392 private PluginPackage _getPluginPackageByURL(String url)
393 throws PluginPackageException {
394
395 String[] repositoryURLs = _getRepositoryURLs();
396
397 for (int i = 0; i < repositoryURLs.length; i++) {
398 String repositoryURL = repositoryURLs[i];
399
400 try {
401 RemotePluginPackageRepository repository =
402 _getRepository(repositoryURL);
403
404 return repository.findPluginByArtifactURL(url);
405 }
406 catch (PluginPackageException pe) {
407 _log.error("Unable to load repository " + repositoryURL, pe);
408 }
409 }
410
411 return null;
412 }
413
414 private RemotePluginPackageRepository _getRepository(
415 String repositoryURL)
416 throws PluginPackageException {
417
418 RemotePluginPackageRepository repository = _repositoryCache.get(
419 repositoryURL);
420
421 if (repository != null) {
422 return repository;
423 }
424
425 return _loadRepository(repositoryURL);
426 }
427
428 private String[] _getRepositoryURLs() throws PluginPackageException {
429 try {
430 String[] trusted = PrefsPropsUtil.getStringArray(
431 PropsKeys.PLUGIN_REPOSITORIES_TRUSTED, StringPool.NEW_LINE,
432 PropsValues.PLUGIN_REPOSITORIES_TRUSTED);
433 String[] untrusted = PrefsPropsUtil.getStringArray(
434 PropsKeys.PLUGIN_REPOSITORIES_UNTRUSTED, StringPool.NEW_LINE,
435 PropsValues.PLUGIN_REPOSITORIES_UNTRUSTED);
436
437 return ArrayUtil.append(trusted, untrusted);
438 }
439 catch (Exception e) {
440 throw new PluginPackageException(
441 "Unable to read repository list", e);
442 }
443 }
444
445 private String[] _getStatusAndInstalledVersion(
446 PluginPackage pluginPackage) {
447
448 PluginPackage installedPluginPackage =
449 _installedPluginPackages.getLatestPluginPackage(
450 pluginPackage.getGroupId(), pluginPackage.getArtifactId());
451
452 String status = null;
453 String installedVersion = null;
454
455 if (installedPluginPackage == null) {
456 status = PluginPackageImpl.STATUS_NOT_INSTALLED;
457 }
458 else {
459 installedVersion = installedPluginPackage.getVersion();
460
461 if (installedPluginPackage.isLaterVersionThan(pluginPackage)) {
462 status = PluginPackageImpl.STATUS_NEWER_VERSION_INSTALLED;
463 }
464 else if (installedPluginPackage.isPreviousVersionThan(
465 pluginPackage)) {
466
467 status = PluginPackageImpl.STATUS_OLDER_VERSION_INSTALLED;
468 }
469 else {
470 status = PluginPackageImpl.STATUS_SAME_VERSION_INSTALLED;
471 }
472 }
473
474 return new String[] {status, installedVersion};
475 }
476
477 private String[] _getSupportedTypes() {
478 return PropsValues.PLUGIN_TYPES;
479 }
480
481 private void _indexPluginPackage(PluginPackage pluginPackage) {
482 String[] statusAndInstalledVersion =
483 _getStatusAndInstalledVersion(pluginPackage);
484
485 String status = statusAndInstalledVersion[0];
486 String installedVersion = statusAndInstalledVersion[1];
487
488 try {
489 PluginPackageIndexer.updatePluginPackage(
490 pluginPackage.getModuleId(), pluginPackage.getName(),
491 pluginPackage.getVersion(), pluginPackage.getModifiedDate(),
492 pluginPackage.getAuthor(), pluginPackage.getTypes(),
493 pluginPackage.getTags(), pluginPackage.getLicenses(),
494 pluginPackage.getLiferayVersions(),
495 pluginPackage.getShortDescription(),
496 pluginPackage.getLongDescription(),
497 pluginPackage.getChangeLog(), pluginPackage.getPageURL(),
498 pluginPackage.getRepositoryURL(), status, installedVersion);
499 }
500 catch (Exception e) {
501 _log.error("Error reindexing " + pluginPackage.getModuleId(), e);
502 }
503 }
504
505 private boolean _isCurrentVersionSupported(List<String> versions) {
506 Version currentVersion = Version.getInstance(ReleaseInfo.getVersion());
507
508 for (String version : versions) {
509 Version supportedVersion = Version.getInstance(version);
510
511 if (supportedVersion.includes(currentVersion)) {
512 return true;
513 }
514 }
515
516 return false;
517 }
518
519 private boolean _isIgnored(PluginPackage pluginPackage)
520 throws SystemException {
521
522 String packageId = pluginPackage.getPackageId();
523
524 String[] pluginPackagesIgnored = PrefsPropsUtil.getStringArray(
525 PropsKeys.PLUGIN_NOTIFICATIONS_PACKAGES_IGNORED,
526 StringPool.NEW_LINE,
527 PropsValues.PLUGIN_NOTIFICATIONS_PACKAGES_IGNORED);
528
529 for (int i = 0; i < pluginPackagesIgnored.length; i++) {
530 String curPluginPackagesIgnored = pluginPackagesIgnored[i];
531
532 if (curPluginPackagesIgnored.endsWith(StringPool.STAR)) {
533 String prefix = curPluginPackagesIgnored.substring(
534 0, curPluginPackagesIgnored.length() - 2);
535
536 if (packageId.startsWith(prefix)) {
537 return true;
538 }
539 }
540 else {
541 if (packageId.equals(curPluginPackagesIgnored)) {
542 return true;
543 }
544 }
545 }
546
547 return false;
548 }
549
550 private boolean _isInstallationInProcess(String context) {
551 if (_installedPluginPackages.getInstallingPluginPackage(
552 context) != null) {
553
554 return true;
555 }
556 else {
557 return false;
558 }
559 }
560
561 private boolean _isTrusted(String repositoryURL)
562 throws PluginPackageException {
563
564 try {
565 String[] trusted = PrefsPropsUtil.getStringArray(
566 PropsKeys.PLUGIN_REPOSITORIES_TRUSTED, StringPool.NEW_LINE,
567 PropsValues.PLUGIN_REPOSITORIES_TRUSTED);
568
569 if (ArrayUtil.contains(trusted, repositoryURL)) {
570 return true;
571 }
572 else {
573 return false;
574 }
575 }
576 catch (Exception e) {
577 throw new PluginPackageException(
578 "Unable to read repository list", e);
579 }
580 }
581
582 private boolean _isUpdateAvailable() throws SystemException {
583 if (!PrefsPropsUtil.getBoolean(
584 PropsKeys.PLUGIN_NOTIFICATIONS_ENABLED,
585 PropsValues.PLUGIN_NOTIFICATIONS_ENABLED)) {
586
587 return false;
588 }
589
590 if (_updateAvailable != null) {
591 return _updateAvailable.booleanValue();
592 }
593 else if (!_settingUpdateAvailable) {
594 _settingUpdateAvailable = true;
595
596 Thread indexerThread = new Thread(
597 new UpdateAvailableRunner(), PluginPackageUtil.class.getName());
598
599 indexerThread.setPriority(Thread.MIN_PRIORITY);
600
601 indexerThread.start();
602 }
603
604 return false;
605 }
606
607 private RemotePluginPackageRepository _loadRepository(String repositoryURL)
608 throws PluginPackageException {
609
610 RemotePluginPackageRepository repository = null;
611
612 StringBuilder sb = new StringBuilder();
613
614 if (!repositoryURL.startsWith(Http.HTTP_WITH_SLASH) &&
615 !repositoryURL.startsWith(Http.HTTPS_WITH_SLASH)) {
616
617 sb.append(Http.HTTP_WITH_SLASH);
618 }
619
620 sb.append(repositoryURL);
621 sb.append(StringPool.SLASH);
622 sb.append(REPOSITORY_XML_FILENAME_PREFIX);
623 sb.append(StringPool.DASH);
624 sb.append(ReleaseInfo.getVersion());
625 sb.append(StringPool.PERIOD);
626 sb.append(REPOSITORY_XML_FILENAME_EXTENSION);
627
628 String pluginsXmlURL = sb.toString();
629
630 try {
631 HttpImpl httpImpl = (HttpImpl)HttpUtil.getHttp();
632
633 HostConfiguration hostConfig = httpImpl.getHostConfig(
634 pluginsXmlURL);
635
636 HttpClient client = httpImpl.getClient(hostConfig);
637
638 GetMethod getFileMethod = new GetMethod(pluginsXmlURL);
639
640 byte[] bytes = null;
641
642 try {
643 int responseCode = client.executeMethod(
644 hostConfig, getFileMethod);
645
646 if (responseCode != HttpServletResponse.SC_OK) {
647 if (_log.isDebugEnabled()) {
648 _log.debug(
649 "A repository for version " +
650 ReleaseInfo.getVersion() + " was not found. " +
651 "Checking general repository");
652 }
653
654 sb = new StringBuilder();
655
656 sb.append(repositoryURL);
657 sb.append(StringPool.SLASH);
658 sb.append(REPOSITORY_XML_FILENAME_PREFIX);
659 sb.append(StringPool.PERIOD);
660 sb.append(REPOSITORY_XML_FILENAME_EXTENSION);
661
662 pluginsXmlURL = sb.toString();
663
664 getFileMethod.releaseConnection();
665
666 getFileMethod = new GetMethod(pluginsXmlURL);
667
668 responseCode = client.executeMethod(
669 hostConfig, getFileMethod);
670
671 if (responseCode != HttpServletResponse.SC_OK) {
672 throw new PluginPackageException(
673 "Unable to download file " + pluginsXmlURL +
674 " because of response code " + responseCode);
675 }
676 }
677
678 bytes = getFileMethod.getResponseBody();
679 }
680 finally {
681 getFileMethod.releaseConnection();
682 }
683
684 if ((bytes != null) && (bytes.length > 0)) {
685 repository = _parseRepositoryXml(
686 new String(bytes), repositoryURL);
687
688 _repositoryCache.put(repositoryURL, repository);
689 _availableTagsCache.addAll(repository.getTags());
690 _lastUpdateDate = new Date();
691 _updateAvailable = null;
692
693 return repository;
694 }
695 else {
696 _lastUpdateDate = new Date();
697
698 throw new PluginPackageException("Download returned 0 bytes");
699 }
700 }
701 catch (MalformedURLException mue) {
702 _repositoryCache.remove(repositoryURL);
703
704 throw new PluginPackageException(
705 "Invalid URL " + pluginsXmlURL, mue);
706 }
707 catch (IOException ioe) {
708 _repositoryCache.remove(repositoryURL);
709
710 throw new PluginPackageException(
711 "Unable to communicate with repository " + repositoryURL, ioe);
712 }
713 catch (DocumentException de) {
714 _repositoryCache.remove(repositoryURL);
715
716 throw new PluginPackageException(
717 "Unable to parse plugin list for repository " + repositoryURL,
718 de);
719 }
720 }
721
722 private RemotePluginPackageRepository _parseRepositoryXml(
723 String xml, String repositoryURL)
724 throws DocumentException {
725
726 List<String> supportedPluginTypes = Arrays.asList(getSupportedTypes());
727
728 if (_log.isDebugEnabled()) {
729 _log.debug(
730 "Loading plugin repository " + repositoryURL + ":\n" + xml);
731 }
732
733 RemotePluginPackageRepository pluginPackageRepository =
734 new RemotePluginPackageRepository(repositoryURL);
735
736 if (xml == null) {
737 return pluginPackageRepository;
738 }
739
740 Document doc = SAXReaderUtil.read(xml);
741
742 Element root = doc.getRootElement();
743
744 Properties settings = _readProperties(
745 root.element("settings"), "setting");
746
747 pluginPackageRepository.setSettings(settings);
748
749 Iterator<Element> itr1 = root.elements("plugin-package").iterator();
750
751 while (itr1.hasNext()) {
752 Element pluginPackageEl = itr1.next();
753
754 PluginPackage pluginPackage = _readPluginPackageXml(
755 pluginPackageEl);
756
757 if (!_isCurrentVersionSupported(
758 pluginPackage.getLiferayVersions())) {
759
760 continue;
761 }
762
763 Iterator<String> itr2 = pluginPackage.getTypes().iterator();
764
765 boolean containsSupportedTypes = false;
766
767 while (itr2.hasNext()) {
768 String type = itr2.next();
769
770 if (supportedPluginTypes.contains(type)) {
771 containsSupportedTypes = true;
772
773 break;
774 }
775 }
776
777 if (!containsSupportedTypes) {
778 continue;
779 }
780
781 pluginPackage.setRepository(pluginPackageRepository);
782
783 pluginPackageRepository.addPluginPackage(pluginPackage);
784
785 _indexPluginPackage(pluginPackage);
786 }
787
788 return pluginPackageRepository;
789 }
790
791 private Date _readDate(String text) {
792 if (Validator.isNotNull(text)) {
793 DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
794 Time.RFC822_FORMAT, Locale.US);
795
796 try {
797 return dateFormat.parse(text);
798 }
799 catch (Exception e) {
800 if (_log.isWarnEnabled()) {
801 _log.warn("Unable to parse date " + text);
802 }
803 }
804 }
805
806 return new Date();
807 }
808
809 private String _readHtml(String text) {
810 return GetterUtil.getString(text);
811 }
812
813 private List<License> _readLicenseList(Element parentEL, String name) {
814 List<License> licenses = new ArrayList<License>();
815
816 Iterator<Element> itr = parentEL.elements(name).iterator();
817
818 while (itr.hasNext()) {
819 Element licenseEl = itr.next();
820
821 License license = new License();
822
823 license.setName(licenseEl.getText());
824
825 Attribute osiApproved = licenseEl.attribute("osi-approved");
826
827 if (osiApproved != null) {
828 license.setOsiApproved(
829 GetterUtil.getBoolean(osiApproved.getText()));
830 }
831
832 Attribute url = licenseEl.attribute("url");
833
834 if (url != null) {
835 license.setUrl(url.getText());
836 }
837
838 licenses.add(license);
839 }
840
841 return licenses;
842 }
843
844 private List<String> _readList(Element parentEl, String name) {
845 List<String> result = new ArrayList<String>();
846
847 if (parentEl != null) {
848 Iterator<Element> itr = parentEl.elements(name).iterator();
849
850 while (itr.hasNext()) {
851 Element el = itr.next();
852
853 String text = el.getText().trim().toLowerCase();
854
855 result.add(text);
856 }
857 }
858
859 return result;
860 }
861
862 private PluginPackage _readPluginPackageProperties(
863 String displayName, Properties properties) {
864
865 int pos = displayName.indexOf("-portlet");
866
867 String pluginType = Plugin.TYPE_PORTLET;
868
869 if (pos == -1) {
870 pos = displayName.indexOf("-hook");
871
872 pluginType = Plugin.TYPE_HOOK;
873 }
874
875 if (pos == -1) {
876 pos = displayName.indexOf("-layouttpl");
877
878 pluginType = Plugin.TYPE_LAYOUT_TEMPLATE;
879 }
880
881 if (pos == -1) {
882 pos = displayName.indexOf("-theme");
883
884 pluginType = Plugin.TYPE_THEME;
885 }
886
887 if (pos == -1) {
888 pos = displayName.indexOf("-web");
889
890 pluginType = Plugin.TYPE_WEB;
891 }
892
893 if (pos == -1) {
894 return null;
895 }
896
897 String displayPrefix = displayName.substring(0, pos);
898
899 String moduleGroupId = GetterUtil.getString(
900 properties.getProperty("module-group-id"));
901 String moduleArtifactId = displayPrefix + "-" + pluginType;
902
903 String moduleVersion = null;
904
905 int moduleVersionPos = pos + pluginType.length() + 2;
906
907 if (displayName.length() > moduleVersionPos) {
908 moduleVersion = displayName.substring(moduleVersionPos);
909 }
910 else {
911 moduleVersion = ReleaseInfo.getVersion();
912 }
913
914 String moduleId =
915 moduleGroupId + "/" + moduleArtifactId + "/" + moduleVersion +
916 "/war";
917
918 String pluginName = GetterUtil.getString(
919 properties.getProperty("name"));
920
921 String deploymentContext = GetterUtil.getString(
922 properties.getProperty("recommended-deployment-context"),
923 moduleArtifactId);
924
925 String author = GetterUtil.getString(properties.getProperty("author"));
926
927 List<String> types = new ArrayList<String>();
928
929 types.add(pluginType);
930
931 List<License> licenses = new ArrayList<License>();
932
933 String[] licensesArray = StringUtil.split(
934 properties.getProperty("licenses"));
935
936 for (int i = 0; i < licensesArray.length; i++) {
937 License license = new License();
938
939 license.setName(licensesArray[i].trim());
940 license.setOsiApproved(true);
941
942 licenses.add(license);
943 }
944
945 List<String> liferayVersions = new ArrayList<String>();
946
947 String[] liferayVersionsArray = StringUtil.split(
948 properties.getProperty("liferay-versions"));
949
950 for (String liferayVersion : liferayVersionsArray) {
951 liferayVersions.add(liferayVersion.trim());
952 }
953
954 if (liferayVersions.size() == 0) {
955 liferayVersions.add(ReleaseInfo.getVersion() + "+");
956 }
957
958 List<String> tags = new ArrayList<String>();
959
960 String[] tagsArray = StringUtil.split(properties.getProperty("tags"));
961
962 for (String tag : tagsArray) {
963 tags.add(tag.trim());
964 }
965
966 String shortDescription = GetterUtil.getString(
967 properties.getProperty("short-description"));
968 String longDescription = GetterUtil.getString(
969 properties.getProperty("long-description"));
970 String changeLog = GetterUtil.getString(
971 properties.getProperty("change-log"));
972 String pageURL = GetterUtil.getString(
973 properties.getProperty("page-url"));
974 String downloadURL = GetterUtil.getString(
975 properties.getProperty("download-url"));
976
977 PluginPackage pluginPackage = new PluginPackageImpl(moduleId);
978
979 pluginPackage.setName(pluginName);
980 pluginPackage.setRecommendedDeploymentContext(deploymentContext);
981 pluginPackage.setAuthor(author);
983 pluginPackage.setTypes(types);
984 pluginPackage.setLicenses(licenses);
985 pluginPackage.setLiferayVersions(liferayVersions);
986 pluginPackage.setTags(tags);
987 pluginPackage.setShortDescription(shortDescription);
988 pluginPackage.setLongDescription(longDescription);
989 pluginPackage.setChangeLog(changeLog);
990 pluginPackage.setPageURL(pageURL);
992 pluginPackage.setDownloadURL(downloadURL);
993
995 return pluginPackage;
996 }
997
998 private PluginPackage _readPluginPackageXml(String xml)
999 throws DocumentException {
1000
1001 Document doc = SAXReaderUtil.read(xml);
1002
1003 Element root = doc.getRootElement();
1004
1005 return _readPluginPackageXml(root);
1006 }
1007
1008 private PluginPackage _readPluginPackageXml(Element pluginPackageEl) {
1009 String name = pluginPackageEl.elementText("name");
1010
1011 if (_log.isDebugEnabled()) {
1012 _log.debug("Reading pluginPackage definition " + name);
1013 }
1014
1015 PluginPackage pluginPackage = new PluginPackageImpl(
1016 GetterUtil.getString(pluginPackageEl.elementText("module-id")));
1017
1018 List<String> liferayVersions = _readList(
1019 pluginPackageEl.element("liferay-versions"), "liferay-version");
1020
1021 List<String> types = _readList(
1022 pluginPackageEl.element("types"), "type");
1023
1024 pluginPackage.setName(_readText(name));
1025 pluginPackage.setRecommendedDeploymentContext(
1026 _readText(
1027 pluginPackageEl.elementText("recommended-deployment-context")));
1028 pluginPackage.setModifiedDate(
1029 _readDate(pluginPackageEl.elementText("modified-date")));
1030 pluginPackage.setAuthor(
1031 _readText(pluginPackageEl.elementText("author")));
1032 pluginPackage.setTypes(types);
1033 pluginPackage.setLicenses(
1034 _readLicenseList(
1035 pluginPackageEl.element("licenses"), "license"));
1036 pluginPackage.setLiferayVersions(liferayVersions);
1037 pluginPackage.setTags(
1038 _readList(pluginPackageEl.element("tags"), "tag"));
1039 pluginPackage.setShortDescription(
1040 _readText(pluginPackageEl.elementText("short-description")));
1041 pluginPackage.setLongDescription(
1042 _readHtml(pluginPackageEl.elementText("long-description")));
1043 pluginPackage.setChangeLog(
1044 _readHtml(pluginPackageEl.elementText("change-log")));
1045 pluginPackage.setScreenshots(
1046 _readScreenshots(pluginPackageEl.element("screenshots")));
1047 pluginPackage.setPageURL(
1048 _readText(pluginPackageEl.elementText("page-url")));
1049 pluginPackage.setDownloadURL(
1050 _readText(pluginPackageEl.elementText("download-url")));
1051 pluginPackage.setDeploymentSettings(
1052 _readProperties(
1053 pluginPackageEl.element("deployment-settings"), "setting"));
1054
1055 return pluginPackage;
1056 }
1057
1058 private Properties _readProperties(Element parentEl, String name) {
1059 Properties result = new Properties();
1060
1061 if (parentEl != null) {
1062 Iterator<Element> itr = parentEl.elements(name).iterator();
1063
1064 while (itr.hasNext()) {
1065 Element el = itr.next();
1066
1067 result.setProperty(
1068 el.attribute("name").getValue(),
1069 el.attribute("value").getValue());
1070 }
1071 }
1072
1073 return result;
1074 }
1075
1076 private List<Screenshot> _readScreenshots(Element parentEl) {
1077 List<Screenshot> screenshots = new ArrayList<Screenshot>();
1078
1079 if (parentEl != null) {
1080 Iterator<Element> itr = parentEl.elements("screenshot").iterator();
1081
1082 while (itr.hasNext()) {
1083 Element screenshotEl = itr.next();
1084
1085 Screenshot screenshot = new Screenshot();
1086
1087 screenshot.setThumbnailURL(
1088 screenshotEl.element("thumbnail-url").getText());
1089 screenshot.setLargeImageURL(
1090 screenshotEl.element("large-image-url").getText());
1091
1092 screenshots.add(screenshot);
1093 }
1094 }
1095
1096 return screenshots;
1097 }
1098
1099 private String _readText(String text) {
1100 return HtmlUtil.extractText(GetterUtil.getString(text));
1101 }
1102
1103 private void _refreshUpdatesAvailableCache() {
1104 _updateAvailable = null;
1105 }
1106
1107 private void _reIndex() throws SystemException {
1108 if (SearchEngineUtil.isIndexReadOnly()) {
1109 return;
1110 }
1111
1112 try {
1113 PluginPackageIndexer.cleanIndex();
1114
1115 for (PluginPackage pluginPackage :
1116 _getAllAvailablePluginPackages()) {
1117
1118 String[] statusAndInstalledVersion =
1119 _getStatusAndInstalledVersion(pluginPackage);
1120
1121 String status = statusAndInstalledVersion[0];
1122 String installedVersion = statusAndInstalledVersion[1];
1123
1124 com.liferay.portal.kernel.search.Document doc =
1125 PluginPackageIndexer.getPluginPackageDocument(
1126 pluginPackage.getModuleId(), pluginPackage.getName(),
1127 pluginPackage.getVersion(),
1128 pluginPackage.getModifiedDate(),
1129 pluginPackage.getAuthor(), pluginPackage.getTypes(),
1130 pluginPackage.getTags(), pluginPackage.getLicenses(),
1131 pluginPackage.getLiferayVersions(),
1132 pluginPackage.getShortDescription(),
1133 pluginPackage.getLongDescription(),
1134 pluginPackage.getChangeLog(),
1135 pluginPackage.getPageURL(),
1136 pluginPackage.getRepositoryURL(), status,
1137 installedVersion);
1138
1139 SearchEngineUtil.addDocument(CompanyConstants.SYSTEM, doc);
1140 }
1141 }
1142 catch (SystemException se) {
1143 throw se;
1144 }
1145 catch (Exception e) {
1146 throw new SystemException(e);
1147 }
1148 }
1149
1150 private RepositoryReport _reloadRepositories() throws SystemException {
1151 if (_log.isInfoEnabled()) {
1152 _log.info("Reloading repositories");
1153 }
1154
1155 RepositoryReport repositoryReport = new RepositoryReport();
1156
1157 String[] repositoryURLs = _getRepositoryURLs();
1158
1159 for (int i = 0; i < repositoryURLs.length; i++) {
1160 String repositoryURL = repositoryURLs[i];
1161
1162 try {
1163 _loadRepository(repositoryURL);
1164
1165 repositoryReport.addSuccess(repositoryURL);
1166 }
1167 catch (PluginPackageException pe) {
1168 repositoryReport.addError(repositoryURL, pe);
1169
1170 _log.error(
1171 "Unable to load repository " + repositoryURL + " " +
1172 pe.toString());
1173 }
1174
1175 }
1176
1177 _reIndex();
1178
1179 return repositoryReport;
1180 }
1181
1182 private void _registerInstalledPluginPackage(
1183 PluginPackage pluginPackage) {
1184
1185 _installedPluginPackages.addPluginPackage(pluginPackage);
1186
1187 _updateAvailable = null;
1188
1189 _indexPluginPackage(pluginPackage);
1190 }
1191
1192 private void _registerPluginPackageInstallation(
1193 String preliminaryContext) {
1194
1195 _installedPluginPackages.registerPluginPackageInstallation(
1196 preliminaryContext);
1197 }
1198
1199 private Hits _search(
1200 String keywords, String type, String tag, String license,
1201 String repositoryURL, String status, int start, int end)
1202 throws SystemException {
1203
1204 _checkRepositories(repositoryURL);
1205
1206 try {
1207 BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
1208
1209 contextQuery.addRequiredTerm(
1210 Field.PORTLET_ID, PluginPackageIndexer.PORTLET_ID);
1211
1212 BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
1213
1214 fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
1215
1216 if (Validator.isNotNull(keywords)) {
1217 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
1218
1219 searchQuery.addTerm(Field.TITLE, keywords);
1220 searchQuery.addTerm(Field.CONTENT, keywords);
1221
1222 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
1223 }
1224
1225 if (Validator.isNotNull(type)) {
1226 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
1227
1228 searchQuery.addExactTerm("type", type);
1229
1230 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
1231 }
1232
1233 if (Validator.isNotNull(tag)) {
1234 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
1235
1236 searchQuery.addExactTerm("tag", tag);
1237
1238 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
1239 }
1240
1241 if (Validator.isNotNull(repositoryURL)) {
1242 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
1243
1244 Query query = TermQueryFactoryUtil.create(
1245 "repositoryURL", repositoryURL);
1246
1247 searchQuery.add(query, BooleanClauseOccur.SHOULD);
1248
1249 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
1250 }
1251
1252 if (Validator.isNotNull(license)) {
1253 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
1254
1255 searchQuery.addExactTerm("license", license);
1256
1257 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
1258 }
1259
1260 if (Validator.isNotNull(status) && !status.equals("all")) {
1261 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
1262
1263 if (status.equals(PluginPackageImpl.
1264 STATUS_NOT_INSTALLED_OR_OLDER_VERSION_INSTALLED)) {
1265
1266 searchQuery.addExactTerm(
1267 "status", PluginPackageImpl.STATUS_NOT_INSTALLED);
1268 searchQuery.addExactTerm(
1269 "status",
1270 PluginPackageImpl.STATUS_OLDER_VERSION_INSTALLED);
1271 }
1272 else {
1273 searchQuery.addExactTerm("status", status);
1274 }
1275
1276 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
1277 }
1278
1279 return SearchEngineUtil.search(
1280 CompanyConstants.SYSTEM, fullQuery, start, end);
1281 }
1282 catch (Exception e) {
1283 throw new SystemException(e);
1284 }
1285 }
1286
1287 private void _unregisterInstalledPluginPackage(
1288 PluginPackage pluginPackage) {
1289
1290 _installedPluginPackages.removePluginPackage(pluginPackage);
1291
1292 try {
1293 List<PluginPackage> pluginPackages = _getAvailablePluginPackages(
1294 pluginPackage.getGroupId(), pluginPackage.getArtifactId());
1295
1296 for (PluginPackage availablePackage : pluginPackages) {
1297 _indexPluginPackage(availablePackage);
1298 }
1299 }
1300 catch (PluginPackageException ppe) {
1301 if (_log.isWarnEnabled()) {
1302 _log.warn(
1303 "Unable to reindex unistalled package " +
1304 pluginPackage.getContext() + ": " + ppe.getMessage());
1305 }
1306 }
1307 }
1308
1309 private void _updateInstallingPluginPackage(
1310 String preliminaryContext, PluginPackage pluginPackage) {
1311
1312 _installedPluginPackages.unregisterPluginPackageInstallation(
1313 preliminaryContext);
1314 _installedPluginPackages.registerPluginPackageInstallation(
1315 pluginPackage);
1316 }
1317
1318 private static Log _log = LogFactoryUtil.getLog(PluginPackageUtil.class);
1319
1320 private static PluginPackageUtil _instance = new PluginPackageUtil();
1321
1322 private LocalPluginPackageRepository _installedPluginPackages;
1323 private Map<String, RemotePluginPackageRepository> _repositoryCache;
1324 private Set<String> _availableTagsCache;
1325 private Date _lastUpdateDate;
1326 private Boolean _updateAvailable;
1327 private boolean _settingUpdateAvailable;
1328
1329 private class UpdateAvailableRunner implements Runnable {
1330
1331 public void run() {
1332 try {
1333 setUpdateAvailable();
1334 }
1335 catch (Exception e) {
1336 if (_log.isWarnEnabled()) {
1337 _log.warn(e.getMessage());
1338 }
1339 }
1340 }
1341
1342 protected void setUpdateAvailable() throws Exception {
1343 StopWatch stopWatch = null;
1344
1345 if (_log.isInfoEnabled()) {
1346 _log.info("Checking for available updates");
1347
1348 stopWatch = new StopWatch();
1349
1350 stopWatch.start();
1351 }
1352
1353 for (PluginPackage pluginPackage :
1354 _installedPluginPackages.getPluginPackages()) {
1355
1356 PluginPackage availablePluginPackage = null;
1357
1358 if (_isIgnored(pluginPackage)) {
1359 continue;
1360 }
1361
1362 availablePluginPackage =
1363 PluginPackageUtil.getLatestAvailablePluginPackage(
1364 pluginPackage.getGroupId(),
1365 pluginPackage.getArtifactId());
1366
1367 if (availablePluginPackage == null) {
1368 continue;
1369 }
1370
1371 Version availablePluginPackageVersion = Version.getInstance(
1372 availablePluginPackage.getVersion());
1373
1374 if (availablePluginPackageVersion.isLaterVersionThan(
1375 pluginPackage.getVersion())) {
1376
1377 _updateAvailable = Boolean.TRUE;
1378
1379 break;
1380 }
1381 }
1382
1383 if (_updateAvailable == null) {
1384 _updateAvailable = Boolean.FALSE;
1385 }
1386
1387 _settingUpdateAvailable = false;
1388
1389 if (_log.isInfoEnabled()) {
1390 _log.info(
1391 "Finished checking for available updates in " +
1392 stopWatch.getTime() + " ms");
1393 }
1394 }
1395 }
1396
1397}