Richard S. Hall | 85bafab | 2009-07-13 13:25:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package org.cauldron.bld.ivy; |
| 21 | |
| 22 | import java.util.Map; |
| 23 | import java.util.Properties; |
| 24 | |
| 25 | import org.cauldron.bld.core.BldCore; |
| 26 | import org.cauldron.sigil.model.IModelElement; |
| 27 | import org.cauldron.sigil.model.eclipse.ISigilBundle; |
| 28 | import org.cauldron.sigil.repository.IResolution; |
| 29 | import org.cauldron.sigil.repository.IResolutionMonitor; |
| 30 | import org.cauldron.sigil.repository.ResolutionConfig; |
| 31 | import org.cauldron.sigil.repository.ResolutionException; |
| 32 | |
| 33 | public class BldResolver implements IBldResolver { |
| 34 | private Map<String, Properties> repos; |
| 35 | private BldRepositoryManager manager; |
| 36 | |
| 37 | static { |
| 38 | try { |
| 39 | BldCore.init(); |
| 40 | } catch (Exception e) { |
| 41 | // TODO Auto-generated catch block |
| 42 | e.printStackTrace(); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | public BldResolver(Map<String,Properties> repos) { |
| 47 | this.repos = repos; |
| 48 | } |
| 49 | |
| 50 | public IResolution resolve(IModelElement element, boolean transitive) { |
| 51 | int options = ResolutionConfig.IGNORE_ERRORS | ResolutionConfig.INCLUDE_OPTIONAL; |
| 52 | if (transitive) options |= ResolutionConfig.INCLUDE_DEPENDENTS; |
| 53 | |
| 54 | ResolutionConfig config = new ResolutionConfig(options); |
| 55 | try { |
| 56 | return resolve(element, config); |
| 57 | } catch (ResolutionException e) { |
| 58 | throw new IllegalStateException("eek! this shouldn't happen when ignoreErrors=true", e); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public IResolution resolveOrFail(IModelElement element, boolean transitive) throws ResolutionException { |
| 63 | int options = 0; |
| 64 | if ( transitive ) options |= ResolutionConfig.INCLUDE_DEPENDENTS; |
| 65 | ResolutionConfig config = new ResolutionConfig(options); |
| 66 | return resolve(element, config); |
| 67 | } |
| 68 | |
| 69 | private IResolution resolve(IModelElement element, ResolutionConfig config) throws ResolutionException { |
| 70 | if (manager == null) { |
| 71 | manager = new BldRepositoryManager(repos); |
| 72 | } |
| 73 | |
| 74 | IResolutionMonitor nullMonitor = new IResolutionMonitor() { |
| 75 | public void endResolution(IModelElement requirement, ISigilBundle sigilBundle) { |
| 76 | } |
| 77 | |
| 78 | public boolean isCanceled() { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | public void startResolution(IModelElement requirement) { |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | return manager.getBundleResolver().resolve(element, config, nullMonitor); |
| 87 | } |
| 88 | } |