1 /*
2 Copyright (C) 2007 Grid Systems, S.A.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 package com.gridsystems.nextgrid.api.pom.components;
19
20 import java.net.URI;
21 import java.util.List;
22 import java.util.UUID;
23
24 import nextgrid.api.env.ProcessSelector;
25 import nextgrid.api.env.SelectionException;
26 import nextgrid.api.pom.AbstractProcess;
27 import nextgrid.api.pom.Process;
28
29 /**
30 * Implementation of very simple selector and prioritiser interfaces.
31 *
32 * @author Rodrigo Ruiz
33 */
34 public class DefaultSelector implements ProcessSelector {
35
36 /**
37 * Selector URI.
38 */
39 private URI id;
40
41 /**
42 * Creates a new instance.
43 */
44 public DefaultSelector() {
45 this.id = genRandomURI();
46 }
47
48 /**
49 * {@inheritDoc}
50 */
51 public URI getId() {
52 return this.id;
53 }
54
55
56 /**
57 * {@inheritDoc}
58 */
59 public void select(Process p) throws SelectionException {
60 if (p instanceof AbstractProcess) {
61 AbstractProcess ap = (AbstractProcess)p;
62 List<Process> list = ap.getCandidates();
63 if (list == null || list.size() == 0) {
64 throw new SelectionException("No candidates to select among");
65 } else {
66 ap.setSelected(list.get(0));
67 }
68 }
69 }
70
71 /**
72 * {@inheritDoc}
73 */
74 public void setId(URI id) {
75 this.id = id;
76 }
77
78
79 /**
80 * Generates a random URI in the UUID name space.
81 *
82 * @return An UUID encapsulated within a URI
83 */
84 private static URI genRandomURI() {
85 UUID uuid = UUID.randomUUID();
86 try {
87 return new URI("urn:uuid:" + uuid);
88 } catch (Exception e) {
89 throw new InternalError(e.toString());
90 }
91 }
92 }