Skip to content

Commit 143d18f

Browse files
author
Daman Arora
committed
use coffeeManager and it's implementation instead of service
1 parent a57c45e commit 143d18f

3 files changed

Lines changed: 210 additions & 18 deletions

File tree

plugins/hackerbook/feature/src/main/java/org/apache/cloudstack/coffee/CoffeeService.java renamed to plugins/hackerbook/feature/src/main/java/org/apache/cloudstack/api/CoffeeManager.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,21 @@
1717
* under the License.
1818
*/
1919

20-
package org.apache.cloudstack.coffee;
20+
package org.apache.cloudstack.api;
2121

22-
import com.cloud.utils.component.PluggableService;
23-
import org.apache.cloudstack.api.command.ListCoffeeCmd;
2422
import org.apache.cloudstack.api.command.CreateCoffeeCmd;
25-
import org.apache.cloudstack.api.command.UpdateCoffeeCmd;
23+
import org.apache.cloudstack.api.command.ListCoffeeCmd;
2624
import org.apache.cloudstack.api.command.RemoveCoffeeCmd;
25+
import org.apache.cloudstack.api.command.UpdateCoffeeCmd;
2726

28-
import java.util.ArrayList;
2927
import java.util.List;
3028

31-
public class CoffeeService implements PluggableService {
29+
public interface CoffeeManager {
30+
Coffee createCoffee(CreateCoffeeCmd cmd);
31+
32+
List<Coffee> listCoffees(ListCoffeeCmd cmd);
33+
34+
Coffee updateCoffee(UpdateCoffeeCmd cmd);
3235

33-
@Override
34-
public List<Class<?>> getCommands() {
35-
List<Class<?>> cmdList = new ArrayList<>();
36-
cmdList.add(CreateCoffeeCmd.class);
37-
cmdList.add(ListCoffeeCmd.class);
38-
cmdList.add(UpdateCoffeeCmd.class);
39-
cmdList.add(RemoveCoffeeCmd.class);
40-
return cmdList;
41-
}
42-
}
36+
boolean removeCoffee(RemoveCoffeeCmd cmd);
37+
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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.apache.cloudstack.feature;
21+
22+
import com.cloud.utils.component.ManagerBase;
23+
import com.cloud.utils.component.PluggableService;
24+
import org.apache.cloudstack.api.Coffee;
25+
import org.apache.cloudstack.api.CoffeeManager;
26+
import org.apache.cloudstack.api.command.CreateCoffeeCmd;
27+
import org.apache.cloudstack.api.command.ListCoffeeCmd;
28+
import org.apache.cloudstack.api.command.RemoveCoffeeCmd;
29+
import org.apache.cloudstack.api.command.UpdateCoffeeCmd;
30+
import org.apache.logging.log4j.Logger;
31+
import org.apache.logging.log4j.LogManager;
32+
33+
import javax.naming.ConfigurationException;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
import java.util.Map;
37+
38+
public class CoffeeManagerImpl extends ManagerBase implements CoffeeManager, PluggableService {
39+
40+
private static final Logger LOGGER = LogManager.getLogger(CoffeeManagerImpl.class);
41+
42+
private static class HardcodedCoffee implements Coffee {
43+
private final long id;
44+
private final String uuid;
45+
private final String name;
46+
private final Offering offering;
47+
private Size size;
48+
private State state;
49+
50+
public HardcodedCoffee(long id, String uuid, String name, Offering offering, Size size, State state) {
51+
this.id = id;
52+
this.uuid = uuid;
53+
this.name = name;
54+
this.offering = offering;
55+
this.size = size;
56+
this.state = state;
57+
}
58+
59+
@Override
60+
public long getId() { return id; }
61+
62+
@Override
63+
public String getUuid() { return uuid; }
64+
65+
@Override
66+
public String getName() { return name; }
67+
68+
@Override
69+
public Offering getOffering() { return offering; }
70+
71+
@Override
72+
public Size getSize() { return size; }
73+
74+
public void setSize(Size size) { this.size = size; }
75+
76+
@Override
77+
public State getState() { return state; }
78+
79+
public void setState(State state) { this.state = state; }
80+
}
81+
82+
@Override
83+
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
84+
super.configure(name, params);
85+
LOGGER.info("CoffeeManager is being configured");
86+
return true;
87+
}
88+
89+
@Override
90+
public boolean start() {
91+
LOGGER.info("CoffeeManager is starting");
92+
return true;
93+
}
94+
95+
@Override
96+
public boolean stop() {
97+
LOGGER.info("CoffeeManager is stopping");
98+
return true;
99+
}
100+
101+
@Override
102+
public List<Class<?>> getCommands() {
103+
final List<Class<?>> cmdList = new ArrayList<>();
104+
cmdList.add(CreateCoffeeCmd.class);
105+
cmdList.add(ListCoffeeCmd.class);
106+
cmdList.add(UpdateCoffeeCmd.class);
107+
cmdList.add(RemoveCoffeeCmd.class);
108+
return cmdList;
109+
}
110+
111+
@Override
112+
public Coffee createCoffee(CreateCoffeeCmd cmd) {
113+
LOGGER.info("Creating coffee: " + cmd.getName());
114+
Coffee coffee = new HardcodedCoffee(
115+
3L,
116+
"fake-uuid-3",
117+
cmd.getName(),
118+
Coffee.Offering.valueOf(cmd.getOffering()),
119+
Coffee.Size.valueOf(cmd.getSize()),
120+
Coffee.State.Created
121+
);
122+
123+
LOGGER.debug("Created coffee with ID: " + coffee.getId());
124+
return coffee;
125+
}
126+
127+
@Override
128+
public List<Coffee> listCoffees(ListCoffeeCmd cmd) {
129+
LOGGER.info("Listing coffees");
130+
List<Coffee> coffees = new ArrayList<>();
131+
132+
coffees.add(new HardcodedCoffee(1L, "uuid-1", "Espresso",
133+
Coffee.Offering.Espresso, Coffee.Size.SMALL, Coffee.State.Brewed));
134+
135+
coffees.add(new HardcodedCoffee(2L, "uuid-2", "Latte",
136+
Coffee.Offering.Latte, Coffee.Size.LARGE, Coffee.State.Created));
137+
138+
coffees.add(new HardcodedCoffee(3L, "uuid-3", "Cappuccino",
139+
Coffee.Offering.Cappuccino, Coffee.Size.MEDIUM, Coffee.State.Brewing));
140+
141+
String id = cmd.getId();
142+
String offering = cmd.getOffering();
143+
String size = cmd.getSize();
144+
145+
List<Coffee> filtered = new ArrayList<>();
146+
for (Coffee coffee : coffees) {
147+
boolean match = true;
148+
149+
if (id != null && !String.valueOf(coffee.getId()).equals(id)) {
150+
match = false;
151+
}
152+
if (offering != null && !coffee.getOffering().name().equalsIgnoreCase(offering)) {
153+
match = false;
154+
}
155+
if (size != null && !coffee.getSize().name().equalsIgnoreCase(size)) {
156+
match = false;
157+
}
158+
159+
if (match) {
160+
filtered.add(coffee);
161+
}
162+
}
163+
164+
LOGGER.debug("Returning " + filtered.size() + " coffees");
165+
return filtered;
166+
}
167+
168+
@Override
169+
public Coffee updateCoffee(UpdateCoffeeCmd cmd) {
170+
LOGGER.info("Updating coffee with ID: " + cmd.getId());
171+
172+
HardcodedCoffee coffee = new HardcodedCoffee(
173+
Long.parseLong(cmd.getId()),
174+
"uuid-" + cmd.getId(),
175+
"Updated Coffee Order",
176+
Coffee.Offering.Espresso,
177+
cmd.getSize() != null ? Coffee.Size.valueOf(cmd.getSize()) : Coffee.Size.MEDIUM,
178+
Coffee.State.Created
179+
);
180+
181+
LOGGER.debug("Updated coffee: " + coffee.getName());
182+
return coffee;
183+
}
184+
185+
@Override
186+
public boolean removeCoffee(RemoveCoffeeCmd cmd) {
187+
if (cmd.getId() != null) {
188+
LOGGER.info("Removing coffee with ID: " + cmd.getId());
189+
} else if (cmd.getIds() != null) {
190+
LOGGER.info("Removing " + cmd.getIds().size() + " coffees");
191+
}
192+
193+
return true;
194+
}
195+
}

plugins/hackerbook/feature/src/main/resources/META-INF/cloudstack/feature/spring-feature-context.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@
2121
xsi:schemaLocation="http://www.springframework.org/schema/beans
2222
http://www.springframework.org/schema/beans/spring-beans.xsd">
2323

24-
<bean id="coffeeService" class="org.apache.cloudstack.coffee.CoffeeService"/>
25-
</beans>
24+
<!-- CoffeeManager bean - manages coffee order lifecycle -->
25+
<bean id="coffeeManager" class="org.apache.cloudstack.feature.CoffeeManagerImpl" />
26+
27+
</beans>

0 commit comments

Comments
 (0)